HttpClient设置请求头消息User-Agent模拟浏览器

HttpClient设置请求头消息User-Agent模拟浏览器

比如我们请求 www.tuicool.com

用前面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package  com.open1111.httpclient.chap02;
 
import  org.apache.http.HttpEntity;
import  org.apache.http.client.methods.CloseableHttpResponse;
import  org.apache.http.client.methods.HttpGet;
import  org.apache.http.impl.client.CloseableHttpClient;
import  org.apache.http.impl.client.HttpClients;
import  org.apache.http.util.EntityUtils;
 
public  class  Demo01 {
 
     public  static  void  main(String[] args)  throws  Exception{
         CloseableHttpClient httpClient=HttpClients.createDefault();  // 创建httpClient实例
         HttpGet httpGet= new  HttpGet( "http://www.tuicool.com/" ); // 创建httpget实例
         CloseableHttpResponse response=httpClient.execute(httpGet);  // 执行http get请求
         HttpEntity entity=response.getEntity();  // 获取返回实体
         System.out.println( "网页内容:" +EntityUtils.toString(entity,  "utf-8" ));  // 获取网页内容
         response.close();  // response关闭
         httpClient.close();  // httpClient关闭
     }
}

返回内容:

网页内容:<!DOCTYPE html>

<html>

    <head>

          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    </head>

    <body>

        <p>系统检测亲不是真人行为,因系统资源限制,我们只能拒绝你的请求。如果你有疑问,可以通过微博 http://weibo.com/tuicool2012/ 联系我们。</p>

    </body>

</html>


我们模拟下浏览器 设置下User-Agent头消息:

加下 httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"); // 设置请求头消息User-Agent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package  com.open1111.httpclient.chap02;
 
import  org.apache.http.HttpEntity;
import  org.apache.http.client.methods.CloseableHttpResponse;
import  org.apache.http.client.methods.HttpGet;
import  org.apache.http.impl.client.CloseableHttpClient;
import  org.apache.http.impl.client.HttpClients;
import  org.apache.http.util.EntityUtils;
 
public  class  Demo01 {
 
     public  static  void  main(String[] args)  throws  Exception{
         CloseableHttpClient httpClient=HttpClients.createDefault();  // 创建httpClient实例
         HttpGet httpGet= new  HttpGet( "http://www.tuicool.com/" ); // 创建httpget实例
         httpGet.setHeader( "User-Agent" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0" );  // 设置请求头消息User-Agent
         CloseableHttpResponse response=httpClient.execute(httpGet);  // 执行http get请求
         HttpEntity entity=response.getEntity();  // 获取返回实体
         System.out.println( "网页内容:" +EntityUtils.toString(entity,  "utf-8" ));  // 获取网页内容
         response.close();  // response关闭
         httpClient.close();  // httpClient关闭
     }
}

运行 :

QQ鎴浘20170119174538.jpg



当然通过火狐firebug,我们还可以看到其他请求头消息:

QQ鎴浘20170119174605.jpg


都是可以通过setHeader方法 设置key value;来得到模拟浏览器请求;

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值