HttpClient方式模拟http请求设置头

关于HttpClient方式模拟http请求,请求头以及其他参数的设置。

本文就暂时不给栗子了,当作简版参考手册吧。

 

发送请求是设置请求头:header

HttpClient httpClient = new DefaultHttpClient();  
  
        //創建一個httpGet方法   
  
        HttpGet httpGet = new HttpGet("http://www.cnblogs.com/loveyakamoz/archive/2011/07/21/2113252.html"); 
//設置httpGet的头部參數信息 httpGet.setHeader("Accept", "Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpGet.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); httpGet.setHeader("Accept-Encoding", "gzip, deflate"); httpGet.setHeader("Accept-Language", "zh-cn,zh;q=0.5"); httpGet.setHeader("Connection", "keep-alive"); httpGet.setHeader("Cookie", "__utma=226521935.73826752.1323672782.1325068020.1328770420.6;"); httpGet.setHeader("Host", "www.cnblogs.com"); httpGet.setHeader("refer", "http://www.baidu.com/s?tn=monline_5_dg&bs=httpclient4+MultiThreadedHttpConnectionManager"); httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");

 

收到response时,获取头部信息:

     Header headers[] = response.getAllHeaders();  
  
        int i = 0;  
  
        while (i < headers.length) {  
      System.out.println(headers[i].getName() + ":  " + headers[i].getValue());  
                i++;  
        }  

 

// TODO: 2016/5/26   Httpclient方式设置请求头demo

转自:HttpClient中头部Header的使用

posted on 2016-05-26 16:52  是知也  阅读(38590)  评论(0编辑  收藏  举报

导航