最近GFW为了刷存在感,搞得大家是头晕眼花,修改hosts 几乎成了每日必备工作。

索性写了一个小程序,给办公室的同事们分享,其中有个内容 就是抓取网络上的hosts,废了一些周折。

我是在一个博客上抓取的。但是这位朋友的博客应该是在做防盗链,但他的方式比较简单就是5位数的一个整形随机数。这里折腾一下就ok了。

要命的是他这个链接的流类型 居然是gzip。这个郁闷好久,一直以为是编码格式导致解析不出来结果,后来发现是gzip搞的。

主要的一段代码做个记录吧。

 /**
* 网络工具类 用于抓取http://serve.netsh.org上的hosts数据
*
* @author tone
*/
public class NetUtil { private final static String ENCODING = "UTF-8";
private final static String GZIPCODING = "gzip";
private final static String HOST = "http://serve.netsh.org/pub/hosts.php";
private final static String COOKIE = "hostspasscode=%s; Hm_lvt_e26a7cd6079c926259ded8f19369bf0b=1421846509,1421846927,1421847015,1421849633; Hm_lpvt_e26a7cd6079c926259ded8f19369bf0b=1421849633";
private final static String OFF = "off";
private final static String ON = "on";
private final static int RANDOM = 100000;
private static String hostspasscode = null;
private static NetUtil instance; public static NetUtil getInstance() {
if (instance == null) {
instance = new NetUtil();
}
return instance;
} private NetUtil() {
hostspasscode = createRandomCookies();
} /**
* 获取html内容
*
* @param gs
* @param wk
* @param twttr
* @param fb
* @param flkr
* @param dpbx
* @param odrvB
* @param yt
* @param nohl
* @return
*/
public String getHtmlInfo(boolean gs, boolean wk, boolean twttr, boolean fb,
boolean flkr, boolean dpbx, boolean odrv,
boolean yt, boolean nohl) throws Exception {
HttpURLConnection conn = null; String result = ""; //String cookie = "hostspasscode="+hostspasscode+"; Hm_lvt_e26a7cd6079c926259ded8f19369bf0b=1421846509,1421846927,1421847015,1421849633; Hm_lpvt_e26a7cd6079c926259ded8f19369bf0b=1421849633";
String cookie = String.format(COOKIE, hostspasscode); //URL url = new URL("http://serve.netsh.org/pub/hosts.php?passcode=13008&gs=on&wk=on&twttr=on&fb=on&flkr=on&dpbx=on&odrv=on&yt=on&nolh=on");
URL url = new URL(createUrl(hostspasscode, gs, wk, twttr, fb, flkr, dpbx, odrv, yt, nohl));
//System.out.println(cookie);
// System.out.println(url.toString()); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000);
conn.setDoOutput(true);
//get方式提交
conn.setRequestMethod("GET");
//凭借请求头文件
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Cookie", cookie);
conn.setRequestProperty("Host", "serve.netsh.org");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0"); // conn.setRequestProperty("Referer", "http://serve.netsh.org/pub/gethosts.php");
// conn.setRequestProperty("X-Requested-With", "XMLHttpRequest"); conn.connect(); String encoding = conn.getContentEncoding(); result = readStream(conn.getInputStream(), encoding);
//测试进度条显示
// result = readStream(new FileInputStream(new File("/home/tone/Resident.Evil.Damnation.2012.1080p.BluRay.x264.DTS-WiKi.mkv")), "11"); conn.disconnect();
if (nohl) {
result=getLocalHost()+result;
} return result;
} /**
* 读取将InputStream中的字节读以字符的形式取到字符串中,如果encoding是gzip,那么需要先有GZIPInputStream进行封装
*
* @param inputStream InputStream字节流
* @param encoding 编码格式
* @return String类型的形式
* @throws IOException IO异常
*/
private String readStream(InputStream inputStream, String encoding) throws Exception {
StringBuffer buffer = new StringBuffer();
ProgressMonitorInputStream pmis = null; InputStreamReader inputStreamReader = null;
GZIPInputStream gZIPInputStream = null;
if (GZIPCODING.equals(encoding)) {
gZIPInputStream = new GZIPInputStream(inputStream);
inputStreamReader = new InputStreamReader(ProgressUtil.getMonitorInputStream(gZIPInputStream, "获取网络数据"), ENCODING); } else { inputStreamReader = new InputStreamReader(ProgressUtil.getMonitorInputStream(inputStream, "获取网络数据"), ENCODING);
} char[] c = new char[1024]; int lenI;
while ((lenI = inputStreamReader.read(c)) != -1) { buffer.append(new String(c, 0, lenI)); }
if (inputStream != null) {
inputStream.close();
}
if (gZIPInputStream != null) {
gZIPInputStream.close();
}
if (pmis!=null) {
gZIPInputStream.close();
} return buffer.toString(); } /**
* 生成随机Cookies数组
*
* @return 五位随机数字
*/
private String createRandomCookies() { return String.valueOf(Math.random() * RANDOM).substring(0, 5); } /**
* 生成链接字符串
*
* @param hostspasscode
* @param gs
* @param wk
* @param twttr
* @param fb
* @param flkr
* @param dpbx
* @param odrvB
* @param yt
* @param nohl
* @return
*/
private String createUrl(String hostspasscode, boolean gs, boolean wk, boolean twttr, boolean fb,
boolean flkr, boolean dpbx, boolean odrv,
boolean yt, boolean nohl) {
StringBuffer buffer = new StringBuffer();
buffer.append(HOST);
buffer.append("?passcode=" + hostspasscode);
if (gs) {
buffer.append("&gs=" + ON);
} else {
buffer.append("&gs=" + OFF);
}
if (wk) {
buffer.append("&wk=" + ON);
} else {
buffer.append("&wk=" + OFF);
}
if (twttr) {
buffer.append("&twttr=" + ON);
} else {
buffer.append("&twttr=" + OFF);
}
if (fb) {
buffer.append("&fb=" + ON);
} else {
buffer.append("&fb=" + OFF);
}
if (flkr) {
buffer.append("&flkr=" + ON);
} else {
buffer.append("&flkr=" + OFF);
}
if (dpbx) {
buffer.append("&dpbx=" + ON);
} else {
buffer.append("&dpbx=" + OFF);
}
if (odrv) {
buffer.append("&odrv=" + ON);
} else {
buffer.append("&odrv=" + OFF);
}
if (yt) {
buffer.append("&yt=" + ON);
} else {
buffer.append("&yt=" + OFF);
}
if (nohl) {
buffer.append("&nohl=" + ON);
} else {
buffer.append("&nohl=" + OFF);
}
return buffer.toString();
} private String getLocalHost() throws Exception { StringBuffer buffer=new StringBuffer();
String hostName=OSUtil.getInstance().getLocalhostName();
buffer.append("#LOCALHOST begin"+"\n");
buffer.append("127.0.0.1\tlocalhost"+"\n");
if (hostName!=null&&!"".equals(hostName)) {
buffer.append("127.0.1.1\t"+hostName+"\n");
} buffer.append("#LOCALHOST end"+"\n");
return buffer.toString(); } }

Java HttpURLConnection 抓取网页内容 解析gzip格式输入流数据并转换为String格式字符串的更多相关文章

  1. 网络爬虫Java实现抓取网页内容

    package 抓取网页; import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream; ...

  2. 【Azure 环境】在Windows环境中抓取网络包(netsh trace)后,如何转换为Wireshark格式以便进行分析

    问题描述 如何在Windows环境中,不安装第三方软件的情况下(使用Windows内置指令),如何抓取网络包呢?并且如何转换为Wireshark 格式呢? 操作步骤 1) 以管理员模式打开CMD,使用 ...

  3. iOS—网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据

    网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...

  4. iOS开发——网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据

    网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...

  5. paip.抓取网页内容--java php python

    paip.抓取网页内容--java php python.txt 作者Attilax  艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...

  6. 使用Jsoup函数包抓取网页内容

    之前写过一篇用Java抓取网页内容的文章,当时是用url.openStream()函数创建一个流,然后用BufferedReader把这个inputstream读取进来.抓取的结果是一整个字符串.如果 ...

  7. 使用Python中的urlparse、urllib抓取和解析网页(一)(转)

    对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过Python 语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...

  8. HTTPCLIENT抓取网页内容

    通过httpclient抓取网页信息. public class SnippetHtml{ /** * 通过url获取网站html * @param url 网站url */ public Strin ...

  9. 爬虫学习一系列:urllib2抓取网页内容

    爬虫学习一系列:urllib2抓取网页内容 所谓网页抓取,就是把URL地址中指定的网络资源从网络中读取出来,保存到本地.我们平时在浏览器中通过网址浏览网页,只不过我们看到的是解析过的页面效果,而通过程 ...

随机推荐

  1. 基于HTML5 SVG炫酷文字爆炸特效

    这是一款使用html5 svg.css3和js制作的炫酷文字爆炸特效.该文字特效用SVG path属性将文字路径切割为很多小块,然后使用css3和js在鼠标滑过文字时制作文字爆炸分裂的炫酷效果. 在线 ...

  2. java中如何忽略字符串中的转义字符--转载

    原文地址:http://my.oschina.net/u/1010578/blog/366252 起因     这几天工作上需要跟另一个同事联调rest接口,我这边是java他是php,返回报文是js ...

  3. spring security源码分析之web包分析

    Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案.一般来说,Web 应用的安全性包括 ...

  4. JSON特殊字符处理

    JSON 是适用于 Ajax 应用程序的一种有效格式,原因是它使 JavaScript 对象和字符串值之间得以快速转换.由于 Ajax 应用程序非常适合将纯文本发送给服务器端程序并对应地接收纯文本,相 ...

  5. WPF之复杂形状控件

    有的时候想将一张图片变成一个按钮.当然这里不是单纯讲图片作为按钮的背景. 这两者是有区别的: 前者图片即按钮,比如你有一个空心的圆圈,当你点击中间空心部分的时候是没有任何反应的因为它不是属于按钮的一部 ...

  6. Amazon 开始接受 Windows 礼品卡预订

    在 8 月微软虚拟货币系统 Microsoft Points 已经正式被真实货币替代,但目前,配套真实货币系统将推出的礼品卡还并没有开始销售.Amazon 上的一则预订显示“Windows 礼品卡”( ...

  7. mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

    1:添加控制器类文件HomeController.cs其代码如下: using System; using System.Collections.Generic; using System.Linq; ...

  8. 批量更新memcached缓存

    假如系统里有3类数据company,user,product 利用维护版本号version的方式达到批量更新缓存的效果 memcache.Add("company",cversio ...

  9. Parallel.ForEach() 并行循环

    现在的电脑几乎都是多核的,但在软件中并还没有跟上这个节奏,大多数软件还是采用传统的方式,并没有很好的发挥多核的优势. 微软的并行运算平台(Microsoft’s Parallel Computing ...

  10. 【Linux/Ubuntu学习 12】ubuntu下对/etc/profile误修改导致系统不能登录

    etc/profile里设置环境变量导致无法登录解决   1,因为不小心在 etc/profile里设在环境变量导致无法登录    不要在 etc/profile里设置 export PATH这样会导 ...