22

我找不到关于 httpclient 4.1 的默认 httpParams 的任何文档?

当我执行 GET 时,默认的套接字超时是多少?

4

5 回答 5

25

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

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

于 2012-03-16T09:38:00.020 回答
25

接受的答案不适用于较新版本的 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
于 2017-01-09T16:12:56.967 回答
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();
于 2018-08-16T12:14:32.343 回答
3

我正在观察无限超时。我正在使用 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()

于 2021-01-29T14:15:33.670 回答
1

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

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

默认值:-1

于 2017-03-29T07:55:37.153 回答