private String doGet(String url, String encode)
throws ClientProtocolException, IOException {
String result = "";
try {
HttpGet httpRequst = new HttpGet(url);
// httpRequst.addHeader("Content-Type", "text/html;charset=" +
// encode);
// httpRequst.getParams().setParameter(
// CoreProtocolPNames.HTTP_CONTENT_CHARSET, encode);
DefaultHttpClient httpClient = new DefaultHttpClient();
// httpClient.getParams().setParameter(
// CoreProtocolPNames.HTTP_CONTENT_CHARSET, encode);
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 8000);// 连接时间20s
httpClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 8000);// 数据传输时间60s
HttpResponse httpResponse = httpClient.execute(httpRequst);// 其中HttpGet是HttpUriRequst的子类
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity.getContentEncoding() != null) {
if ("gzip".equalsIgnoreCase(httpEntity.getContentEncoding()
.getValue())) {
httpEntity = new GzipDecompressingEntity(httpEntity);
} else if ("deflate".equalsIgnoreCase(httpEntity
.getContentEncoding().getValue())) {
httpEntity = new DeflateDecompressingEntity(httpEntity);
}
}
result = enCodetoString(httpEntity, encode);// 取出应答字符串
} else
httpRequst.abort();
} catch (ClientProtocolException e) {
System.out.println("doget代理读取超时");
} catch (IOException e) {
System.out.println("doget代理读取超时");
}
return result;
}
private String doGetByGoagent(String url, String encode)
throws ClientProtocolException, IOException {
String result = "";
HttpGet httpRequst = new HttpGet(url);
httpRequst.addHeader("Accept-Encoding", "gzip,deflate,sdch");
httpRequst.getParams().setParameter(
CoreProtocolPNames.HTTP_CONTENT_CHARSET, encode);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost proxyHost = new HttpHost("127.0.0.1", 8087, null);
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 8000);// 连接时间20s
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
6000);// 数据传输时间60s
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,
proxyHost);
HttpResponse httpResponse = httpClient.execute(httpRequst);// 其中HttpGet是HttpUriRequst的子类
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity.getContentEncoding() != null) {
if ("gzip".equalsIgnoreCase(httpEntity.getContentEncoding()
.getValue())) {
httpEntity = new GzipDecompressingEntity(httpEntity);
} else if ("deflate".equalsIgnoreCase(httpEntity
.getContentEncoding().getValue())) {
httpEntity = new DeflateDecompressingEntity(httpEntity);
}
}
result = enCodetoString(httpEntity, encode);// 取出应答字符串
} else
httpRequst.abort();
return result;
} |