获取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);
获取URL列表,设置代理请求URL,https的加密方式处理的更多相关文章
- 微信小程序设置全局请求URL 封装wx.request请求
app.js: App({ //设置全局请求URL globalData:{ URL: 'https://www.oyhdo.com', }, /** * 封装wx.request请求 * metho ...
- [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换
[Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换 问题现象: 碰到一个问题,UI交互表现为:联通号码在3gwap网络环境下资源一直无法下载成功. 查看Log日志,打印出 ...
- 工作记录之 [ python请求url ] v s [ java请求url ]
背景: 模拟浏览器访问web,发送https请求url,为了实验需求需要获取ipv4数据包 由于不做后续的内容整理(有内部平台分析),故只要写几行代码请求发送https请求url列表中的url即可 开 ...
- 【内部】Fiddler设置代理请求的方式
1.2 打开Fiiddler,设置如图步骤: 3.添加规则: 4.这里选择第三个选项: 5.选中^开始,空格结束的如图内容.复制你要代理的地址.如:http://wap.cmread.com/nap/ ...
- SpringBoot(SpringMVC)使用addViewControllers设置统一请求URL重定向配置
只需要在配置中重写 addViewControllers方法 import org.springframework.context.annotation.Configuration; import o ...
- http和https的加密方式
BS盛行的今天有点网络只是很必要啊,首先需要个网络抓包工具wireshark, http:http通过三次握手来通信,握手过程看图1 https:https = http + ssl(secure s ...
- scrapy设置代理的方法
方法一: 直接在spider文件下设置代理,通过传参的方式设置在Request中 import scrapy class MimvpSpider(scrapy.spiders.Spider): nam ...
- .net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包
前言: 通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddl ...
- 通过zabbix的API接口获取服务器列表
Zabbix API说明 1) 基于Web的API,作为Web前端的一部分提供,使用JSON-RPC 2.0协议 2) 身份认证Token:在访问Zabbix中的任何数据之前,需要登录并获取身份验证令 ...
随机推荐
- C# 7.0 新特性4: 返回引用
本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
- Debian8修改启动默认运行级别
Two things you need to know: 1) Systemd boots towards the target given by "default.target" ...
- Spring3+Mybatis3+Mysql+ivy+liquibase
Spring3+Mybatis3+Mysql+ivy+liquibase 集成 近一周时间所学技术:整合Spring+MyBatis+MySql+ivy+liquibase Mybatis:是一个基于 ...
- javascript正则表达式验证IP,URL
验证IP function isIP(ipstr){ var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[ ...
- Dll的显式和隐式调用
建立项目,请选择Win32 控制台项目(Win32 Console Application),选择DLL和空项目选项.DLLs可能并不如你想像的那样难.首先写你的头文件(header file):称为 ...
- Python基础-函数篇
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 函数与函数式编程 1.面向对象: 华山派-- ...
- MyBatis学习--SqlMapConfig.xml配置文件
简介 SqlMapConfig.xml是MyBatis的全局配置文件,在前面的文章中我们可以看出,在SqlMapConfig.xml主要是配置了数据源.事务和映射文件,其实在SqlMapConfig. ...
- canvas三角函数直线运动
var canvas = document.getElementById("canvas"); var cxt = canvas.getContext("2d" ...
- svn做目录访问控制(AuthzSVNAccessFile)
这个是Apache的配置文件 加载模块和svn的相关设置
- ES6 变量的解构赋值
数组的解构赋值 var [a,b,c] = [1,2,3]; 左边是变量,右边是值,根据数据结构一一对应 只要等号两边的模式相同,左边的变量就会被赋予右边对应的值,必须模式相同 如果等号 ...