Java Apache HttpClient EnityUtils块
我目前正在从事一个使用ApacheHttpClient 4.1.2的项目,它从一个网站检索一些数据
应用程序的作用:它转到一个网页,然后转到下一个(找到的)网页,直到它到达末尾(例如:转到第1页->;再找到20页->;每隔20页转到一页)。问题是,它在检索一些随机页面时陷入困境,无法继续爬网
下面是一些代码:
DefaultHttpClient mainHttp;
HttpPost post;
HttpResponse response;
HttpEntity entity;
String s;
int curPage = 1;
int index = 0;
boolean ok = true;
...
while (ok) {
response = mainHttp.execute(post);
entity = response.getEntity();
if (entity != null) {
System.out.println("Enter " + curPage);
s = EntityUtils.toString(entity);
System.out.println("Exit " + curPage);
index = s.indexOf("[" + curPage + "]");
if (index > 0) {
parseContent();
} else {
ok = false;
}
}
}
在“调试”窗口上显示如下内容:
Enter 1
Exit 1
.
.
.
Enter n
我也在使用http请求分析器,我在Stuck的页面上看到,数据没有被完全检索(它没有到达</html>
或页面的末尾)
在这种情况下,如何跳过或重试下载数据?有人能帮我吗
谢谢大家!
LE:
实际设置为:
mainHttp.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(1, true));
mainHttp.getParams().setParameter("http.connection-manager.timeout", 15000);
mainHttp.getParams().setParameter("http.socket.timeout", 15000);
mainHttp.getParams().setParameter("http.connection.timeout", 15000);
其中15000
是以毫秒为单位的超时
谢谢你的帮助
# 1 楼答案
来源:http://hc.apache.org/httpclient-3.x/tutorial.html(方法恢复)
但这只是在出现任何异常的情况下,请在每次发出请求时尝试检查IOExceptions