2011-05-16 129 views

回答

75

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(); 
+6

您需要添加'requestBuilder.setSocketTimeout(timeout);'。請注意'timeout'應該是毫秒! – Stefan 2014-08-29 10:41:43

+2

注意,調用'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

+4

此外,生成器允許您鏈更簡單的代碼這些語句:'RequestConfig配置= RequestConfig.custom()setConnectTimeout(超時).setSocketTimeout(超時).build();' – Jonik 2015-02-10 09:53:12

4

section 2.5你看如何設置CONNECTION_TIMEOUT參數的例子。

CONNECTION_TIMEOUT是等待初始連接的時間和SO_TIMEOUT是您等待建立連接之後讀取數據包時的超時。

8

從@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); 
+0

又見靜態方法HttpConnectionParams.setConnectionTimeout()和HttpConnectionParams.setSoTimeout(),它提供了一個簡便的方法來完成同樣的事情。 – dnault 2013-01-24 20:44:05

+0

謝謝。我已經用這個選項更新了答案。 – quux00 2013-01-29 20:55:41

+0

非常有幫助。非常感謝!!! – 2015-02-01 07:35:05

7

在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); 
1

我設置了硬超時整個請求解決該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) 
相關問題