(java)urlconnection访问ContentEncoding为gzip的网页

(转载)http://blog.csdn.net/yaoweijq/article/details/6088699

幸好java.util.zip包下提供了GZIPxxxx

可以直接调用,

ContentEncoding为text/html或其他类似的文本直接打印出来就行

为gzip的话,需要再转化一下

上程序:

String url = "http://health.sohu.com/yangshengtang/";

				URL cumtURL = new URL(
						url);

				HttpURLConnection cumtConnection = (HttpURLConnection)cumtURL.openConnection();
				cumtConnection.setRequestProperty("User-Agent",
						"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13");
				cumtConnection.setRequestProperty("Pragma", "no-cache");
				cumtConnection.setRequestProperty("Proxy-Connection", "Keep-Alive");
				cumtConnection.setRequestProperty("Host", "health.sohu.com");
				//Cookie	Hm_lvt_9f14aaa038bbba8b12ec2a4a3e51d254=1287650788640; BAIDUID=F25ABEDD87D60C8104D0CF4D75A71979:FG=1; bdime=0; BD_UTK_DVT=1; USERID=fdb673a39972591f66d93243
				cumtConnection.setRequestProperty("Cookie", "YYID=C52798160DB2F2EB9A5C522772777A83; SUV=0812111111371033; vjuids=3f340bb23.11ed3e18865.0.7e372d1208008; vjlast=1231915420,1231915420,30.1292830395.10; IPLOC=CN6101");
				cumtConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
				cumtConnection.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
				cumtConnection.setRequestProperty("Pragma", "no-cache");
				cumtConnection.setRequestProperty("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
				cumtConnection.setRequestProperty("Accept-Encoding", "gzip,deflate");
				System.out.println(cumtConnection.getContentEncoding());
//				InputStream urlStream = cumtConnection.getInputStream();
				InputStream urlStream = new GZIPInputStream(cumtConnection.getInputStream());
				BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream,"gb2312"));
				String line = "";
				while((line = reader.readLine()) != null) {
					System.out.println(line);
				}
				reader.close();
				urlStream.close();


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值