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版本为例,不同版本可能有所不同
/** * Retus 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() {retu 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() {retu 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() {retu 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单位是毫秒
作者:russle
来源链接:https://blog.csdn.net/russle/article/details/126064154
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。