HttpClient设置请求头消息User-Agent模拟浏览器
比如我们请求 www.tuicool.com
用前面的代码:
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
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关闭 } }
运行 :
当然通过火狐firebug,我们还可以看到其他请求头消息:
都是可以通过setHeader方法 设置key value;来得到模拟浏览器请求;