最近在开发一个小的爬虫工具,在登陆成功以后页面会302重定向到主页,如果我们只是简单的使用httpclient的话,会没有办法获取到数据的结果.其实经过简单的设置就可以了.直接看代码:
ClientConnectionManager cm = new PoolingClientConnectionManager();
DefaultHttpClient client = new DefaultHttpClient(cm);
client.setRedirectStrategy(new DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
boolean isRedirect = false;
try {
isRedirect = super.isRedirected(request, response, context);
} catch (ProtocolException e) {
e.printStackTrace();
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
}
return isRedirect;
}
});