HttpClient connectTimeout和socketTImeout

3,300 阅读1分钟

HttpClient是HTTP的工具包,是基于Socket封装的。在使用过程中自己总是分不清connectTIme和socketTime的区别。所以记录下。

参考官方文档就能跟容易知道:

getConnectTimeout()
Determines the timeout in milliseconds until a connection is established.

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).

connectTimeout

指的是建立连接的时间,也就是进行三次握手,建立tcp连接的时间。

socketTimeout

指的是等待数据的时间,或者两次数据包之间的时间间隔。

这两种超时时间如果设置为0,则表示无限长时间。在建立socket连接时,若底层网络层确认不可达,会立即返回SocketException异常,不是一直重试到connectTimeout设定值。