首页 技术 正文
技术 2022年11月19日
0 收藏 684 点赞 4,494 浏览 2414 个字

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

  1.当使用第一节的代码 来 访问推酷的时候,会返回给我们如下信息:

网页内容:<!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模拟浏览器。代码如下:

/**
* 抓取网页信息使用 get请求
* @param args
* @throws IOException
* @throws ClientProtocolException
*/
public static void main(String[] args) throws ClientProtocolException, IOException {
// 创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httpGet实例
HttpGet httpGet = new HttpGet("http://www.tuicool.com");
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
CloseableHttpResponse response = httpClient.execute(httpGet);
if(response != null){
HttpEntity entity = response.getEntity(); // 获取网页内容
String result = EntityUtils.toString(entity, "UTF-8");
System.out.println("网页内容:" + result);
}
if(response != null){
response.close();
}
if(httpClient != null){
httpClient.close();
}
}

  给HttpGet方法设置头消息,即可模拟浏览器访问。

二、获取响应内容Content-Type  

  使用  entity.getContentType().getValue()  来获取Content-Type,代码如下:

public static void main(String[] args) throws ClientProtocolException, IOException {
// 创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httpGet实例
HttpGet httpGet = new HttpGet("http://www.tuicool.com");
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
CloseableHttpResponse response = httpClient.execute(httpGet);
if(response != null){
HttpEntity entity = response.getEntity(); // 获取网页内容
System.out.println("Content-Type:" + entity.getContentType().getValue()); // 获取Content-Type
}
if(response != null){
response.close();
}
if(httpClient != null){
httpClient.close();
}
}

三、获取响应状态

  200 — 正常

  403 — 拒绝

  500 — 服务器报错

  400 — 未找到页面

  使用 response.getStatusLine().getStatusCode() 获取响应状态,代码如下:

public static void main(String[] args) throws ClientProtocolException, IOException {
// 创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httpGet实例
HttpGet httpGet = new HttpGet("http://www.tuicool.com");
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
CloseableHttpResponse response = httpClient.execute(httpGet);
if(response != null){
int state = response.getStatusLine().getStatusCode();
System.out.println("响应状态:" + state);
}
if(response != null){
response.close();
}
if(httpClient != null){
httpClient.close();
}
}

四、HttpClient学习地址

  开源博客系统-HttpClient

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894