HttpComponent客户端的默认超时

thi*_*one 14 java apache-httpcomponents

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

我做GET时默认的套接字超时是多少?

ben*_*y23 19

根据文档,该http.socket.timeout参数控制SO_TIMEOUT值,并且:

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

  • 此设置从4.3.X版本更改.默认为系统超时. (5认同)

Cha*_*dru 13

接受的答案不适用于较新版本的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
Run Code Online (Sandbox Code Playgroud)

  • 如何获取系统默认值? (6认同)

Mik*_*phy 7

对于 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();
Run Code Online (Sandbox Code Playgroud)