初学httpClient,想去抓取某个网页查询出来的结果信息,通过浏览器监控发现response出来的信息和java控制台打印出来的不一样。
package com.test;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
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 Test2 {
public static void main(String[] args) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
public String handleResponse(final HttpResponse response)
throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity,
"utf-8") : null;
} else {
throw new ClientProtocolException(
"Unexpected response status: " + status);
}
}
};
HttpGet httpGet = new HttpGet("http://zhixing.court.gov.cn/search");
String strResult = httpclient.execute(httpGet, responseHandler);
System.out.println(strResult);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
看你传的useragent是什么,最好和你的浏览器的一致,应该是服务器端有判断ua。
您好,我现在也正在爬取这个网站,请问你解决问题了吗?
你好,我通过获取cookie已经解决这个问题:)。
楼主解决这个问题了嘛?我知道大概是因为cookies,不过我不清楚该怎么做。
楼主解决这个问题了嘛?我知道大概是因为cookies,不过我不清楚该怎么做。
楼主解决这个问题了嘛?我知道大概是因为cookies,不过我不清楚该怎么做。