如何在httpcomponents httpclient中設置連接超時?我在http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html找到了這些文檔,但不清楚這些參數是如何設置的。Apache HttpComponents HttpClient超時
此外,SO_TIMEOUT
和CONNECTION_TIMEOUT
之間的區別的解釋將是有益的。
如何在httpcomponents httpclient中設置連接超時?我在http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html找到了這些文檔,但不清楚這些參數是如何設置的。Apache HttpComponents HttpClient超時
此外,SO_TIMEOUT
和CONNECTION_TIMEOUT
之間的區別的解釋將是有益的。
Apache中的HTTP客戶端的4.3版本的配置被重構(AGA在)。新的辦法是這樣的:
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setConnectTimeout(timeout);
requestBuilder = requestBuilder.setConnectionRequestTimeout(timeout);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(requestBuilder.build());
HttpClient client = builder.build();
在section 2.5你看如何設置CONNECTION_TIMEOUT參數的例子。
CONNECTION_TIMEOUT是等待初始連接的時間和SO_TIMEOUT是您等待建立連接之後讀取數據包時的超時。
從@jontro的答案是正確的,但它總是很高興有關於如何做到這一點的代碼片段。有兩種方法可以做到這一點:
版本1:設置10秒的超時每個參數:
HttpClient httpclient = new DefaultHttpClient();
// this one causes a timeout if a connection is established but there is
// no response within 10 seconds
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);
// this one causes a timeout if no connection is established within 10 seconds
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 1000);
// now do the execute:
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
版本2:還爲10秒超時這些參數的每個:
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
HttpConnectionParams.setSoTimeout(params, 10 * 1000);
HttpClient httpclient = new DefaultHttpClient(params);
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
在HttpClient的4.3版本中,你可以使用下面的例子..讓說,持續5秒
int timeout = 5;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout * 1000)
.setConnectionRequestTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000).build();
CloseableHttpClient client =
HttpClientBuilder.create().setDefaultRequestConfig(config).build();
HttpGet request = new HttpGet("http://localhost:8080/service"); // GET Request
response = client.execute(request);
我設置了硬超時整個請求解決該java.net.SocketInputStream.socketRead0
問題。
private static final ScheduledExecutorService SCHEDULED_EXECUTOR = Executors.newSingleThreadScheduledExecutor()
HttpGet request = new HttpGet("http://www.example.com")
final Runnable delayedTask = new Runnable() {
@Override
public void run() {
request.abort()
}
}
SCHEDULED_EXECUTOR.schedule(delayedTask, 100000, TimeUnit.MILLISECONDS)
您需要添加'requestBuilder.setSocketTimeout(timeout);'。請注意'timeout'應該是毫秒! – Stefan 2014-08-29 10:41:43
注意,調用'setConnectTimeout()'和['setConnectionRequestTimeout()'](http://stackoverflow.com/questions/20271017/connection-and-connection-request-timeout#comment30255182_20271424)是不** **同一設置'CONNECTION_TIMEOUT'和'SO_TIMEOUT'(如問題所示)。你可能需要'setConnectTimeout()'和'setSocketTimeout()'。 – Jonik 2015-02-10 09:50:53
此外,生成器允許您鏈更簡單的代碼這些語句:'RequestConfig配置= RequestConfig.custom()setConnectTimeout(超時).setSocketTimeout(超時).build();' – Jonik 2015-02-10 09:53:12