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

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的加密方式处理的更多相关文章

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

    app.js: App({ //设置全局请求URL globalData:{ URL: 'https://www.oyhdo.com', }, /** * 封装wx.request请求 * metho ...

  2. [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换

    [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换 问题现象: 碰到一个问题,UI交互表现为:联通号码在3gwap网络环境下资源一直无法下载成功. 查看Log日志,打印出 ...

  3. 工作记录之 [ python请求url ] v s [ java请求url ]

    背景: 模拟浏览器访问web,发送https请求url,为了实验需求需要获取ipv4数据包 由于不做后续的内容整理(有内部平台分析),故只要写几行代码请求发送https请求url列表中的url即可 开 ...

  4. 【内部】Fiddler设置代理请求的方式

    1.2 打开Fiiddler,设置如图步骤: 3.添加规则: 4.这里选择第三个选项: 5.选中^开始,空格结束的如图内容.复制你要代理的地址.如:http://wap.cmread.com/nap/ ...

  5. SpringBoot(SpringMVC)使用addViewControllers设置统一请求URL重定向配置

    只需要在配置中重写 addViewControllers方法 import org.springframework.context.annotation.Configuration; import o ...

  6. http和https的加密方式

    BS盛行的今天有点网络只是很必要啊,首先需要个网络抓包工具wireshark, http:http通过三次握手来通信,握手过程看图1 https:https = http + ssl(secure s ...

  7. scrapy设置代理的方法

    方法一: 直接在spider文件下设置代理,通过传参的方式设置在Request中 import scrapy class MimvpSpider(scrapy.spiders.Spider): nam ...

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

    前言:  通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddl ...

  9. 通过zabbix的API接口获取服务器列表

    Zabbix API说明 1) 基于Web的API,作为Web前端的一部分提供,使用JSON-RPC 2.0协议 2) 身份认证Token:在访问Zabbix中的任何数据之前,需要登录并获取身份验证令 ...

随机推荐

  1. C# FTP上传

    public class FtpHelper { public FtpHelper(string p_url, string p_user, string p_password) { if (!p_u ...

  2. WPF 绑定枚举值

    前台Xaml <ComboBox x:Name=" HorizontalAlignment="Left" Margin="5 0 0 0" Se ...

  3. 基于.NET Socket API 通信的综合应用

    闲谈一下,最近和客户进行对接Scoket 本地的程序作为请求方以及接受方,对接Scoket 的难度实在比较大,因为涉及到响应方返回的报文的不一致性,对于返回的报文的格式我需要做反序列化的难度增大了不少 ...

  4. .net程序员转行做手游开发经历(一)

    从辞职到自己开发游戏也有几个月的时间了,游戏也已经在AppStore上线了,我觉得我有必要写点东西,算是留下的一些记忆,也可以和广大博友分享下自己的创业经历,这可能不是一篇成功的创业经历,因为故事还在 ...

  5. 从C++研发到前端工程师

    前言 伴随着今天收到了网易的前端offer,我的转行面试告一段落.能拿到网易的offer很意外,也弥补了去年网易校招被刷的遗憾.虽然从c++转行到前端不是一件很困难的事,但是也说不上轻松,反正我用了整 ...

  6. MATLAB中取整函数(fix, floor, ceil, round)的使用

    MATLAB取整函数 1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans = 3    -3(2)floor(x):不超过x 的最大整数.(高斯取整) & ...

  7. 用matlab实现同一个序列重复N倍

    同一个序列 重复N倍 怎么用matlab实现 可以使用repmat函数 repmat(A, 1, 3) 其中A即为复制的矩阵,1为纵向复制的次数,3即为横向复制的次数.

  8. cmd 下telnet 不是内部或外部命令

    问题:cmd 下telnet 提示不是内部或外部命令 解决方案:

  9. [转]Filter实现处理中文乱码,转义html标签,过滤敏感词

    原文地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可 ...

  10. iOS开发小技巧--TableView Group样式中控制每个section之间的距离

    一.TableView的Group样式中,默认的每个section都有sectionHeader和sectionFooter,只要调整这两个的大小就可以实现section之前的间距扩大或缩小 二.项目 ...