HttpComponent客户端的默认超时

时间:2012-03-16 09:04:21

标签: java apache-httpcomponents

我在httpclient 4.1的默认httpParams上找不到任何文档?

进行GET时,默认套接字超时是多少?

5 个答案:

答案 0 :(得分:19)

根据documentationhttp.socket.timeout参数控制SO_TIMEOUT值,并且:

  

如果未设置此参数,则读取操作不会超时   (无限超时)。

答案 1 :(得分:10)

接受的答案不适用于较新版本的HttpClient。 4.3.X及以上版本使用系统默认值,通常为60秒。

取自HttpClient javadoc。

public int getSocketTimeout()
Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets).
A timeout value of zero is interpreted as an infinite timeout. A negative value is interpreted as undefined (system default).

Default: -1

答案 2 :(得分:3)

对于Apache HttpClient 4.x或更高版本

int timeout = 5*60; // seconds (5 minutes)
RequestConfig config = RequestConfig.custom()
      .setConnectTimeout(timeout * 1000)
      .setConnectionRequestTimeout(timeout * 1000)
      .setSocketTimeout(timeout * 1000).build();
HttpClient httpClient = 
   HttpClientBuilder.create().setDefaultRequestConfig(config).build();

答案 3 :(得分:1)

我观察到无限超时。我正在使用 httpclient 4.5.13,它反过来使用 httpcore 4.4.13,其中 SocketConfig 类定义默认值 0(无超时)。 https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/config/SocketConfig.html#getSoTimeout()

答案 4 :(得分:0)

https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.html#getSocketTimeout%28%29

超时值为零被解释为无限超时。负值被解释为未定义(系统默认值)。

默认值:-1