HttpUrlConnection与HttpClient的认识(二)-请求头信息的问题


转载自:http://blog.csdn.net/u010248330/article/details/69258531


上篇博文访问的网络资源是我新建的简单的index.jsp页面,实际工作中我们常常会访问一些网站的资源,比如在一些网站爬虫一些数据等。

本篇博文就简单访问一个博客吧,看看有什么问题。按照我们上一篇博文的介绍,写出下面的代码:

[java]  view plain  copy
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.MalformedURLException;  
  6. import java.net.URL;  
  7. import java.net.URLConnection;  
  8. import java.util.zip.GZIPInputStream;  
  9. import java.io.*;  
  10.   
  11.   
  12. public class TestURLConnection1 {  
  13.   
  14.      public static void main(String[] args) {  
  15.             StringBuffer strBuffer = new StringBuffer();  
  16.             HttpURLConnection httpURLConnection = null;  
  17.             InputStream inputStream = null;  
  18.             BufferedReader rufferedReader = null;  
  19.               
  20.             try {  
  21.                 URL serverUrl = new URL("http://blog.csdn.net/u010248330/article/details/68926199");  
  22.                 httpURLConnection = (HttpURLConnection) serverUrl.openConnection();  
  23.                 System.out.println("请求url的响应状态码:"+httpURLConnection.getResponseCode());  
  24.                 //状态码200是请求成功  
  25.                 if (HttpURLConnection.HTTP_OK == httpURLConnection.getResponseCode()) {  
  26.                     inputStream = httpURLConnection.getInputStream();  
  27.                     rufferedReader = new BufferedReader(new InputStreamReader(  
  28.                             inputStream,"utf-8"));  
  29.                     String str = null;  
  30.                     while ((str = rufferedReader.readLine()) != null) {  
  31.                         strBuffer.append(str);  
  32.                         strBuffer.append("\r\n");  
  33.                     }   
  34.                     System.out.println(strBuffer);  
  35.                }  
  36.             }catch(Exception e) {  
  37.                 e.printStackTrace();  
  38.             }finally{  
  39.                 httpURLConnection.disconnect();  
  40.                 //这里把相关的流也关闭吧,我这里就不写了  
  41.             }  
  42.             }  
  43.       
  44. }  

实验结果:


我们看到返回的状态码是403,访问被禁止了,说明服务器不允许我们的访问,这是怎么回事呢?

我们用浏览器去访问这个博客地址,看看相关的网络信息,主要关注请求的头信息。




从上面图我们应该知道了问题的所在了,这说明我们代码中没有设置相关的请求头信息,博客那边的服务器拒绝了我们的请求,现在我们在代码中利用上一篇博客介绍的方法设置相关的请求头信息看一下:

[java]  view plain  copy
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.MalformedURLException;  
  6. import java.net.URL;  
  7. import java.net.URLConnection;  
  8. import java.util.zip.GZIPInputStream;  
  9. import java.io.*;  
  10.   
  11.   
  12. public class TestURLConnection2 {  
  13.   
  14.      public static void main(String[] args) {  
  15.             StringBuffer strBuffer = new StringBuffer();  
  16.             HttpURLConnection httpURLConnection = null;  
  17.             InputStream inputStream = null;  
  18.             BufferedReader rufferedReader = null;  
  19.               
  20.             try {  
  21.                 URL serverUrl = new URL("http://blog.csdn.net/u010248330/article/details/68926199");  
  22.                 httpURLConnection = (HttpURLConnection) serverUrl.openConnection();  
  23.                 //设置请求的头信息  
  24.                 httpURLConnection.setRequestProperty("Accept-charset""utf-8");  
  25.                 httpURLConnection.setRequestProperty("Accept""text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01");  
  26.                 httpURLConnection.setRequestProperty("Accept-Encoding""gzip, deflate");  
  27.                 httpURLConnection.setRequestProperty("Accept-Language""zh-CN,en-US;q=0.8,zh;q=0.5,en;q=0.3");  
  28.                 httpURLConnection.setRequestProperty("Connection","keep-alive");  
  29.                 httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0");  
  30.                 System.out.println("请求url的响应状态码:"+httpURLConnection.getResponseCode());  
  31.                 //状态码200是请求成功  
  32.                 if (HttpURLConnection.HTTP_OK == httpURLConnection.getResponseCode()) {  
  33.                     inputStream = httpURLConnection.getInputStream();  
  34.                     rufferedReader = new BufferedReader(new InputStreamReader(  
  35.                             inputStream,"utf-8"));  
  36.                     String str = null;  
  37.                     while ((str = rufferedReader.readLine()) != null) {  
  38.                         strBuffer.append(str);  
  39.                         strBuffer.append("\r\n");  
  40.                     }   
  41.                     System.out.println(strBuffer);  
  42.                }  
  43.             }catch(Exception e) {  
  44.                 e.printStackTrace();  
  45.             }finally{  
  46.                 httpURLConnection.disconnect();  
  47.                 //这里把相关的流也关闭吧,我这里就不写了  
  48.             }  
  49.             }  
  50.       
  51. }  

再看输出结果:



     从上的图可以看出,对于我们的url请求,服务器是给我们返回了数据,那怎么都乱码了呢?

     我们打印一下响应的内容的编码信息:System.out.println("服务器响应的内容的编码格式:"+httpURLConnection.getContentEncoding());我们可以看到编码格式是gzip格式,而我们只是用inputStream流去接收数据,并没有解码,所以导致了上面出现的乱码,那怎么解决呢?

     我们在Java.util.zip包下有一个类GZIPInputStream,可用于解码:

[java]  view plain  copy
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.MalformedURLException;  
  6. import java.net.URL;  
  7. import java.net.URLConnection;  
  8. import java.util.zip.GZIPInputStream;  
  9. import java.io.*;  
  10.   
  11.   
  12. public class TestURLConnection3 {  
  13.   
  14.      public static void main(String[] args) {  
  15.             StringBuffer strBuffer = new StringBuffer();  
  16.             HttpURLConnection httpURLConnection = null;  
  17.             InputStream inputStream = null;  
  18.             BufferedReader rufferedReader = null;  
  19.             //用于解码  
  20.             GZIPInputStream gzin = null;  
  21.               
  22.             try {  
  23.                 URL serverUrl = new URL("http://blog.csdn.net/u010248330/article/details/68926199");  
  24.                 httpURLConnection = (HttpURLConnection) serverUrl.openConnection();  
  25.                 //设置请求的头信息  
  26.                 httpURLConnection.setRequestProperty("Accept-charset""utf-8");  
  27.                 httpURLConnection.setRequestProperty("Accept""text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01");  
  28.                 httpURLConnection.setRequestProperty("Accept-Encoding""gzip, deflate");  
  29.                 httpURLConnection.setRequestProperty("Accept-Language""zh-CN,en-US;q=0.8,zh;q=0.5,en;q=0.3");  
  30.                 httpURLConnection.setRequestProperty("Connection","keep-alive");  
  31.                 httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0");  
  32.                 System.out.println("请求url的响应状态码:"+httpURLConnection.getResponseCode());  
  33.                 //状态码200是请求成功  
  34.                 if (HttpURLConnection.HTTP_OK == httpURLConnection.getResponseCode()) {  
  35.                     inputStream = httpURLConnection.getInputStream();  
  36.                     System.out.println("服务器响应的内容的编码格式:"+httpURLConnection.getContentEncoding());  
  37.                     //转码  
  38.                     gzin = new GZIPInputStream(inputStream);  
  39.                     rufferedReader = new BufferedReader(new InputStreamReader(  
  40.                             gzin,"utf-8"));  
  41.                     String str = null;  
  42.                     while ((str = rufferedReader.readLine()) != null) {  
  43.                         strBuffer.append(str);  
  44.                         strBuffer.append("\r\n");  
  45.                     }   
  46.                     System.out.println(strBuffer);  
  47.                }  
  48.             }catch(Exception e) {  
  49.                 e.printStackTrace();  
  50.             }finally{  
  51.                 httpURLConnection.disconnect();  
  52.                 //这里把相关的流也关闭吧,我这里就不写了  
  53.             }  
  54.             }  
  55.       
  56. }  


实验结果:




   这下就完美解决这个问题了。当然我们可以在代码中这样判断一下,再作转换,这样更严谨:

        if (httpURLConnection.getContentEncoding().equals("gzip"))

  在这里我就不改了。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值