HttpClient (https://hc.apache.org/index.html)是我们java开发中经常使用的包之一,其中有三个参数超时的设置经常令人感到困惑,特别是和python request等其他相比较时,不知道如何对应。
参看:https://stackoverflow.com/questions/27749939/httpclient-4-3-5-connectionrequesttimeout-vs-connecttimeout-for-httpconnectionpa
1, 源代码
代码在https://github.com/apache/httpcomponents-client/blob/4.5.x/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java
这里4.5.x版本为例,不同版本可能有所不同
/**
* Returns the timeout in milliseconds used when requesting a connection
* from the connection manager.
* <p>
* A timeout value of zero is interpreted as an infinite timeout.
* A negative value is interpreted as undefined (system default if applicable).
* </p>
* <p>
* Default: {@code -1}
* </p>
*/
public int getConnectionRequestTimeout() {
return connectionRequestTimeout;
}
/**
* Determines the timeout in milliseconds until a connection is established.
* <p>
* A timeout value of zero is interpreted as an infinite timeout.
* A negative value is interpreted as undefined (system default if applicable).
* </p>
* <p>
* Default: {@code -1}
* </p>
*/
public int getConnectTimeout() {
return connectTimeout;
}
/**
* Defines the socket timeout ({@code SO_TIMEOUT}) in milliseconds,
* which is the timeout for waiting for data or, put differently,
* a maximum period inactivity between two consecutive data packets).
* <p>
* A timeout value of zero is interpreted as an infinite timeout.
* A negative value is interpreted as undefined (system default if applicable).
* </p>
* <p>
* Default: {@code -1}
* </p>
*/
public int getSocketTimeout() {
return socketTimeout;
}
2, 解释
-
connectTimeOut:指与服务端建立连接的超时时间,比较容易理解
-
connectionRequestTimeOut:指从连接池获取到连接的超时时间,如果是非连接池的话,该参数暂时没有发现有什么用处
-
socketTimeOut:指客户端和服务端进行数据交互的时候,两者之间多长时间没有数据,而不是整个交互的整体时间,比如如果设置2秒超时,如果每隔1秒传输一次数据,传输10次,总共10秒,这种是不超时的,而如果某次两个数据包之间的间隔时间超过了2秒,则超时。
3, 与python requests比较
https://requests.readthedocs.io/en/latest/user/quickstart/#timeouts
You can tell Requests to stop waiting for a response after a given number of seconds with the timeout parameter. Nearly all production code should use this parameter in nearly all requests. Failure to do so can cause your program to hang indefinitely:
requests.get('https://github.com/', timeout=0.001)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)
Note
timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds). If no timeout is specified explicitly, requests do not time out.
可以看出requests的这个timeout对应和就是httpclient的socketTimeout参数, requests库中的timeout单位是秒,因此可以设置为0.001, 而java httpclient中的timeout单位是毫秒