日期:2014-05-16 浏览次数:20333 次
HttpClient :
?
?
最近要 抓取其他网站的数据, 尝试了 HttpClient
?
http://hc.apache.org/? ?官网提供了很详细了的 例子
?
尝试连接的次数
?
HttpRequestRetryHandler myRetryHandler = new ?HttpRequestRetryHandler() {?
public boolean retryRequest(IOException exception, int executionCount,HttpContext context) {?
System.out.println("尝试连接次数:-------:"+executionCount);
if (executionCount >= 5) {?
// 如果超过最大重试次数,那么就不要继续了?
return false;?
}?
if (exception instanceof NoHttpResponseException) {?
// 如果服务器丢掉了连接,那么就重试?
return true;?
}?
if (exception instanceof SSLHandshakeException) {?
// 不要重试SSL握手异常?
return false;?
}?
?
HttpRequest request = (HttpRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);?
boolean idempotent = !(request instanceof?
HttpEntityEnclosingRequest);?
if (idempotent) {?
// 如果请求被认为是幂等的,那么就重试?
return true;?
}
return false;?
}?
};?
?
?
?
GET:
?
@Test
public void test_OOC(){
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
//设置
httpclient.setHttpRequestRetryHandler(myRetryHandler);?
List<NameValuePair> formparams = new?
ArrayList<NameValuePair>();?
formparams.add(new BasicNameValuePair("eltype", "ct"));?
formparams.add(new BasicNameValuePair("cont_no", "XXX"));?
URI uri = URIUtils.createURI("http", "www.XXXX.com", -1,
"XXXXXXX",?
URLEncodedUtils.format(formparams, "UTF-8"), null);?
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
//获得String 的方法
ResponseHandler<String> responseHandler = new BasicResponseHandler();