作者:丶沈丨灬de艳本人 | 来源:互联网 | 2023-08-20 16:58
Httpclient.setHttpRequestRetryHandler(requestRetryHandler);***设置重连机制和异常自动恢复处理*privatestaticHt
Httpclient.setHttpRequestRetryHandler(requestRetryHandler);
/**
* 设置重连机制和异常自动恢复处理
*/
private static HttpRequestRetryHandler requestRetryHandler = new HttpRequestRetryHandler() {
// 自定义的恢复策略
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
// 设置恢复策略,在Http请求发生异常时候将自动重试3次
if (executionCount >= 3) {
// 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 (exception instanceof SSLHandshakeException) {
// Do not retry on SSL handshake exception
return false;
}
HttpRequest request = (HttpRequest) context
.getAttribute(ExecutionContext.HTTP_REQUEST);
boolean idempotent = (request instanceof HttpEntityEnclosingRequest);
if (!idempotent) {
// Retry if the request is considered idempotent
return true;
}
return false;
}
};