// 设置超时时间
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
// 设置 请求超时为 3 秒
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,3000);
DefaultHttpMethodRetryHandler myHttpRetryHandler = new DefaultHttpMethodRetryHandler() {
public boolean retryMethod(final HttpMethod method,final IOException exception, int executionCount) {
System.out.println("开始自定义网络测试......");
if (executionCount >= 2) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (!method.isRequestSent()) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
// otherwise do not retry
return false;
}
};
postMethod.getParams().setParameter(
HttpMethodParams.RETRY_HANDLER, myHttpRetryHandler);
// 2011-05-27 设置超时时间 结束
// 发请求,获得状态码
statusCode = httpClient.executeMethod(postMethod);