Created
November 28, 2013 09:14
-
-
Save yongchun/7689194 to your computer and use it in GitHub Desktop.
HttpClient处理失败的时候重试
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://hc.apache.org/httpclient-legacy/exception-handling.html