Skip to content

Instantly share code, notes, and snippets.

@yongchun
Created November 28, 2013 09:14
Show Gist options
  • Save yongchun/7689194 to your computer and use it in GitHub Desktop.
Save yongchun/7689194 to your computer and use it in GitHub Desktop.
HttpClient处理失败的时候重试
HttpClient client = new HttpClient();
HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
public boolean retryMethod(
final HttpMethod method,
final IOException exception,
int executionCount) {
if (executionCount >= 5) {
// 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;
}
};
GetMethod httpget = new GetMethod("http://www.whatever.com/");
httpget.getParams().
setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
try {
client.executeMethod(httpget);
System.out.println(httpget.getStatusLine().toString());
} finally {
httpget.releaseConnection();
}
@yongchun
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment