HttpClient的setConnectionRequestTimeout、setConnectTimeout、setSocketTimeout设置注意事项

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, 解释

  1. connectTimeOut:指与服务端建立连接的超时时间,比较容易理解

  2. connectionRequestTimeOut:指从连接池获取到连接的超时时间,如果是非连接池的话,该参数暂时没有发现有什么用处

  3. 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单位是毫秒

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值