初学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();
}
}
}