日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java http超时重连_httpclient 重连机制

發布時間:2024/7/23 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java http超时重连_httpclient 重连机制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們在進行http請求時,難免會遇到請求失敗的情況,失敗后需要重新請求,嘗試再次獲取數據。

Apache的HttpClient提供了異常重試機制,在該機制中,我們可以很靈活的定義在哪些異常情況下進行重試。

今天有個小伙伴,遇到了這樣的問題,后來是通過此方法去解決的,

一個爬蟲,他請求超過了多少次,自動就超時了,目前還沒有找得到具體原因,不過用這個重連機制是完美解決了問題的。好了,代碼如下:

import java.io.IOException;

import java.io.InterruptedIOException;

import java.net.ConnectException;

import java.net.UnknownHostException;

import javax.net.ssl.SSLException;

import org.apache.commons.lang3.ObjectUtils;

import org.apache.commons.lang3.StringUtils;

import org.apache.http.Consts;

import org.apache.http.HttpEntityEnclosingRequest;

import org.apache.http.HttpRequest;

import org.apache.http.HttpStatus;

import org.apache.http.ParseException;

import org.apache.http.client.HttpRequestRetryHandler;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.protocol.HttpClientContext;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.protocol.HttpContext;

import org.apache.http.util.EntityUtils;

public class HttpPostUtils {

public String retryPostJson(String uri, String json, int retryCount, int connectTimeout,

int connectionRequestTimeout, int socketTimeout) throws IOException, ParseException {

if (StringUtils.isAnyBlank(uri, json)) {

return null;

}

HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {

@Override

public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {

if (executionCount > retryCount) {

// Do not retry if over max retry count

return false;

}

if (exception instanceof InterruptedIOException) {

// An input or output transfer has been terminated

return false;

}

if (exception instanceof UnknownHostException) {

// Unknown host 修改代碼讓不識別主機時重試,實際業務當不識別的時候不應該重試,再次為了演示重試過程,執行會顯示retryCount次下面的輸出

System.out.println("不識別主機重試"); return true;

}

if (exception instanceof ConnectException) {

System.out.println("連接超時,正在重新請求ing...."); return true;

}

if (exception instanceof SSLException) {

// SSL handshake exception

return false;

}

HttpClientContext clientContext = HttpClientContext.adapt(context);

HttpRequest request = clientContext.getRequest();

boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);

if (idempotent) {

// Retry if the request is considered idempotent

return true;

}

return false;

}

};

CloseableHttpClient client = HttpClients.custom().setRetryHandler(httpRequestRetryHandler).build();

HttpPost post = new HttpPost(uri);

// Create request data

StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);

// Set request body

post.setEntity(entity);

RequestConfig config = RequestConfig.custom().setConnectTimeout(connectTimeout)

.setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).build();

post.setConfig(config);

// Response content

String responseContent = null;

CloseableHttpResponse response = null;

try {

response = client.execute(post, HttpClientContext.create());

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

responseContent = EntityUtils.toString(response.getEntity(), Consts.UTF_8.name());

}

} finally {

if (ObjectUtils.anyNotNull(response)) {

response.close();

}

if (ObjectUtils.anyNotNull(client)) {

client.close();

}

}

return responseContent;

}

總結

以上是生活随笔為你收集整理的java http超时重连_httpclient 重连机制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。