获取URL列表,设置代理请求URL,https的加密方式处理

做了一个测试的一个小工具,需求如下:

1、有一批URL列表,需要知道哪个URL请求响应内容中包含http:关键字的。

2、url请求包括http和https 2种协议

3、要部署在linux服务器上,且linux服务器只能通过代理来连接外网

帖一下我的核心代码吧:

package com.cn.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
import javax.xml.ws.Response;

import net.sf.json.JSONObject;

public class HttpGet {
    // SSL
    private static class TrustAnyTrustManager implements X509TrustManager {

        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws java.security.cert.CertificateException {
            // TODO Auto-generated method stub

        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws java.security.cert.CertificateException {
            // TODO Auto-generated method stub

        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    // 得到URL
    public List getUrl(String file) {
        BufferedReader bf = null;
        List urlList = new ArrayList();
        int linenum = 0;
        try {
            bf = new BufferedReader(new FileReader(file));
            String info = "";
            while ((info = bf.readLine()) != null) {
                linenum++;
                if (!"".equals(info)) {
                    /*
                     * if ((info.startsWith("http:") ||
                     * info.startsWith("https:"))) {
                     * System.out.println("url===>" + info); urlList.add(info);
                     * }else{ System.out.println("第"+linenum+"行,请求协议或有问题。。:"); }
                     */
                    System.out.println("url===>" + info);
                    urlList.add(info);
                } else {
                    System.out.println("第" + linenum + "行url为空");
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return urlList;
    }

    // 获取请求非200状态的url
    public List<String> http404(List<String> urls) {
        List<String> result404 = new ArrayList<String>();
        try {

            /*
             * System.setProperty("proxySet", "true");
             * System.setProperty("http.proxyHost", "192.168.11.254");
             * System.setProperty("http.proxyPort", "8080");
             */
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                    "192.168.11.254", 8080));

            for (String url2 : urls) {
                if (url2.startsWith("http:") || (url2.startsWith("https:"))) {
                    URL realUrl = new URL(new String(url2.getBytes("utf-8")));
                    HttpURLConnection connection = (HttpURLConnection) realUrl
                            .openConnection(proxy);
                    // 如果是https
                    if (connection instanceof HttpsURLConnection) {
                        SSLContext sc = SSLContext.getInstance("SSL");
                        sc.init(null,
                                new TrustManager[] { new TrustAnyTrustManager() },
                                new java.security.SecureRandom());
                        ((HttpsURLConnection) connection)
                                .setSSLSocketFactory(sc.getSocketFactory());
                        ((HttpsURLConnection) connection)
                                .setHostnameVerifier(new TrustAnyHostnameVerifier());
                    }
                    // 设置通用的请求属性
                    connection.setRequestProperty("accept", "*/*");
                    connection.setRequestProperty("connection", "Keep-Alive");
                    connection
                            .setRequestProperty("user-agent",
                                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                    // 连接
                    connection.connect();

                    if (connection.getResponseCode() != 200) {
                        result404.add(url2);
                    }
                } else {
                    result404.add(url2);
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        return result404;
    }

    // 请求获取响应,把包含搜索关键字的URL存入LIST中
    public List<String> sendUrl(List<String> urls, String keyword) {
        List<String> result200 = new ArrayList<String>();
        BufferedReader in = null;
        try {

            /*
             * System.setProperty("proxySet", "true");
             * System.setProperty("http.proxyHost", "192.168.11.254");
             * System.setProperty("http.proxyPort", "8080");
             */
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                    "192.168.11.254", 8080));
            for (String url2 : urls) {
                StringBuffer response = new StringBuffer();
                if (url2.startsWith("http:") || (url2.startsWith("https:"))) {
                    URL realUrl = new URL(new String(url2.getBytes("utf-8")));
                    HttpURLConnection connection = (HttpURLConnection) realUrl
                            .openConnection(proxy);
                    // 如果是https
                    if (connection instanceof HttpsURLConnection) {
                        SSLContext sc = SSLContext.getInstance("SSL");
                        sc.init(null,
                                new TrustManager[] { new TrustAnyTrustManager() },
                                new java.security.SecureRandom());
                        ((HttpsURLConnection) connection)
                                .setSSLSocketFactory(sc.getSocketFactory());
                        ((HttpsURLConnection) connection)
                                .setHostnameVerifier(new TrustAnyHostnameVerifier());
                    }
                    // 设置通用的请求属性
                    connection.setRequestProperty("accept", "*/*");
                    connection.setRequestProperty("connection", "Keep-Alive");
                    connection
                            .setRequestProperty("user-agent",
                                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                    // 连接
                    connection.connect();

                    if (connection.getResponseCode() == 200) {
                        in = new BufferedReader(new InputStreamReader(
                                connection.getInputStream()));
                        String line = "";
                        while ((line = in.readLine()) != null) {
                            response.append(line);
                        }

                        in.close();
                    }
                    if (response.toString().contains(keyword)) {
                        result200.add(url2);
                    }
                }
                System.out.println(url2 + "=============>"
                        + response.toString());
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

        return result200;
    }

    public static void main(String[] args) {
        HttpGet ht = new HttpGet();
        List urls = ht.getUrl("E:\\课件\\https\\1.txt");
        String keyword = "http:";
        List<String> urllist = ht.http404(urls);
        for (String string : urllist) {
            System.out.println("404=" + string);
        }

    }

}
TrustAnyTrustManager 类中的方法,是我在网上找的,目地是解决SSL加密请求,针对https://协议的请求来处理的。具体是什么我也不懂
/*
             * System.setProperty("proxySet", "true");
             * System.setProperty("http.proxyHost", "192.168.11.254");
             * System.setProperty("http.proxyPort", "8080");
             */这种设置代理的方式,不行的
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                    "192.168.11.254", 8080));
HttpURLConnection connection = (HttpURLConnection) realUrl
                            .openConnection(proxy);
 
时间: 2024-12-25 02:54:23

获取URL列表,设置代理请求URL,https的加密方式处理的相关文章

微信小程序设置全局请求URL 封装wx.request请求

app.js: App({ //设置全局请求URL globalData:{ URL: 'https://www.oyhdo.com', }, /** * 封装wx.request请求 * method: 请求方式 * url: 请求地址 * data: 要传递的参数 * callback: 请求成功回调函数 * errFun: 请求失败回调函数 **/ wxRequest(method, url, data, callback, errFun) { wx.request({ url: url,

从支付宝插件无提示导入根证书带来的安全隐患说开去:谈谈HTTPS的加密方式

众所周知在12306购票,官方说明需要导入根证书,这算是良心的了.其实你安装了支付宝插件,就会被不知情的导入了某些[哔]的根证书,而这会带来一些安全隐患:提升遭遇中间人攻击的可能性:在你完全不知情,以为自己还在享受这HTTPS带来的安全保护的情况下,将未加密的信息完全暴露给第三方(比如GFW).而根证书是什么?为什么可能导入一个证书就可能会遭到中间人攻击的?我们从头说开去~ 一.基本概念 加密: 未加密信息:明文x 加密后信息:密文y 明文到密文之间的转换关系:y=f(x),而解密则是x=f -

JAVA_GET请求URL

1 import java.io.IOException; 2 3 import net.sf.json.JSONObject; 4 5 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; 6 import org.apache.commons.httpclient.Header; 7 import org.apache.commons.httpclient.HttpClient; 8 import org.ap

scrapy设置代理的方法

方法一: 直接在spider文件下设置代理,通过传参的方式设置在Request中 import scrapy class MimvpSpider(scrapy.spiders.Spider): name = "mimvp" allowed_domains = ["mimvp.com"] start_urls = [ "http://proxy.mimvp.com/exist.php", "https://proxy.mimvp.com/

.net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包

原文:.net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包 前言:  通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddler的设置也可以看到: 然后查看本地计算器的网络代理设置: 基于上面的原理,Fiddler就实现了经过本机计算器请求的数据抓包了... 那么,我们通过C#代码,在.net Core中使用HttpClient

fiddler 4 设置代理

Windows使用一种被称为"AppContainer"的隔离技术,它可能会干扰lmmersive应用程序和Edge浏览器的流量捕捉.使用进度Telerik Fiddler的工具栏上的WinConfig按钮来启用流量捕获. 你想学更多吗? 禁用这个警告.点击"取消" fiddler 4 设置代理: Capture HTTPS CONNECTs 捕捉HTTPS连接 Decrypt HTTPS traffic 解密HTTPS交通 from all  processes

C# 设置代理

1.检查代理是否可用: /// <summary> /// 验证代理IP地址是否可用 /// </summary> /// <param name="callback">是否可用</param> /// <param name="ip">地址</param> /// <param name="port">端口</param> public void C

接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头

转自:https://www.cnblogs.com/hong-fithing/p/7617855.html https请求 https协议(Secure Hypertext Transfer Protocol) : 安全超文本传输协议, HTTPS以保密为目标研发, 简单讲HTTPS协议是由SSL+HTTP协议构建的可进行加密传输. 身份认证的网络协议, 其安全基础是SSL协议, 因此加密的详细内容请看SSL. 全称Hypertext Transfer Protocol overSecure

原生socket请求url获取状态码、消息报头、响应正文

需求: 1 (1)使用socket及ssl模块写通用的web客户端 2 (2)向服务器发起请求 3 (3)接受响应内容并解析出状态码.消息报头.响应正文 4 (4)最核心的函数: 输入一个url,返回状态码.消息报头.响应正文:当然这也是最后实现的效果 知识储备: 网络基础知识 python的web编程(socket) 最后实现代码: 1 # __author__ = "wyb" 2 # date: 2018/6/5 3 # 代码: 高内聚低耦合 -> 使用函数封装一些逻辑代码