您的位置:首页 > 理论基础 > 计算机网络

调用httpclient出现[read] I/O error: Read timed out的问题分析和解决办法

2017-12-27 15:19 1041 查看
在使用httpclient 发送http请求时,基本每次都是3秒后,服务器才能收到交易。

日志如下:

2017-12-25 09:08:26,001 [http-nio-8080-exec-8] DEBUG o.a.h.wire - http-outgoing-322 >> "Connection: Keep-Alive[\r][\n]"

2017-12-25 09:08:26,001 [http-nio-8080-exec-8] DEBUG o.a.h.wire - http-outgoing-322 >> "User-Agent: Apache-HttpClient/4.5.1 (Java/1.8.0_144)[\r][\n]"

2017-12-25 09:08:26,001 [http-nio-8080-exec-8] DEBUG o.a.h.wire - http-outgoing-322 >> "Expect: 100-continue[\r][\n]"

2017-12-25 09:08:26,001 [http-nio-8080-exec-8] DEBUG o.a.h.wire - http-outgoing-322 >> "Accept-Encoding: gzip,deflate[\r][\n]"

2017-12-25 09:08:26,001 [http-nio-8080-exec-8] DEBUG o.a.h.wire - http-outgoing-322 >> "[\r][\n]"

2017-12-25 09:08:26,353 [nioEventLoopGroup-700-1] DEBUG i.n.b.PoolThreadCache - Freed 1 thread-local buffer(s) from thread: nioEventLoopGroup-700-1

2017-12-25 09:08:27,986 [nioEventLoopGroup-701-1] DEBUG i.n.b.PoolThreadCache - Freed 2 thread-local buffer(s) from thread: nioEventLoopGroup-701-1

2017-12-25 09:08:29,005 [http-nio-8080-exec-8] DEBUG o.a.h.wire - http-outgoing-322 << "[read] I/O error: Read timed out"

=======================================================================================================================

对此,翻看了源码的报错信息,也没有定位到问题的原因。后来还是通过看日志,在报错前有一段“Expect: 100-continue[\r][\n]”,然后翻看了http 1.0和1.1协议,并结合抓包的分析,才恍然大悟。HTTP 1.1支持只发送header信息(不带任何body信息),如果服务器认为客户端有权限请求服务器,则返回100,否则返回401。客户端如果接受到100,才开始把请求body发送到服务器。

之后,只要将http协议改为1.0,就可以瞬间进行发送并收到应答

参考:     https://www.ietf.org/rfc/rfc2616.txt

                http://blog.csdn.net/kingcodexl/article/details/51306062
http://blog.csdn.net/linsongbin1/article/details/54980801
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐