获取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中的任何数据之前,需要登录并获取身份验证令 ...
随机推荐
- 防止 JavaScript 自动插入分号
JavaScript语言有一个机制:在解析时,能够在一句话后面自动插入一个分号,用来修改语句末尾遗漏的分号分隔符. 然而,由于这个自动插入的分号与JavaScript语言的另一个机制发生了冲突,即所有 ...
- Theano2.1.17-基础知识之剖析theano的函数
来自:http://deeplearning.net/software/theano/tutorial/profiling.html Profiling Theano function note:该方 ...
- DB2和Oracle区别
转 http://blog.chinaunix.net/uid-7374279-id-2057574.html 写在前面:今天客户来访(日本人),问我DB2和Oracle区别.因为不是DBA(勉强的理 ...
- Http协议中的Content-Length属性
Android开发的时候需要与从服务器上获取数据,数据是通过http协议封装的.Android端使用的是Xutils第三方插件来发起http请求,但是每次只能拿到部分数据.通过仔细分析后原来是Cont ...
- iOS -- 隐藏返回按钮
// 隐藏返回按钮 [self.navigationItem setHidesBackButton:YES];
- webservice的常用注解
定义说明书的显示方法1.@WebService(serviceName="PojoService", portName="PojoPort", name=&qu ...
- LINUX下PHP开启短标签short_open_tag支持
LINUX下PHP开启短标签short_open_tag支持 以CENTOS为例: 找到php.ini #find / -name php.ini #/etc/php.ini 编辑php.ini #v ...
- Nginx 的编译安装和URL地址重写
本文转自:http://www.178linux.com/14119#rd?sukey=ecafc0a7cc4a741b573a095a3eb78af6b4c9116b74d0bbc9844d8fc5 ...
- ListView上拉加载,下拉刷新 PullToRefresh的使用
PullToRefresh是一套实现非常好的下拉刷新库,它支持:ListViewExpandableListViewGridViewWebViewScrollViewHorizontalScrollV ...
- [转]response.getWriter().write()与out.print()的区别
原文地址:http://blog.csdn.net/javaloveiphone/article/details/8133772 1.首先介绍write()和print()方法的区别: (1).w ...