<
>

HTTP长连接与短连接使用方法及测试详解

2020-02-07 18:01:05 来源:易采站长站 作者:王振洲

综上,连接的实际有效时间,是根据response的设置来决定的。

其他情况测试

客户端设置Connection: Close

##connection:close请求,kept alive的连接为0
[2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200]
[2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection leased: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 200]
[2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?) - Opening connection {}->http://bizdomain:80
[2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:?) - Connecting to bizdomain/127.0.0.195:80
## 建立新连接
[2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:?) - Connection established 127.0.0.191:49239<->127.0.0.195:80
## 客户端设置短连接
[2017-04-26 13:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: Close[r][n]"
## 服务端返回的也是短连接
[2017-04-26 13:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 << "Connection: close[r][n]"
##请求完之后,关闭连接
[2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.DefaultManagedHttpClientConnection:?) - http-outgoing-0: Close connection
[2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?) - Connection discarded
[2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection released: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200]

如上,当服务端返回Connection: Close时,客户端接收完响应,便会关闭连接。

客户端设置60s超时,服务端设置5s超时

##Keep-Alive: timeout=60 第一次请求,与connection:close无差别
[2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200]
[2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection leased: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 200]
[2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?) - Opening connection {}->http://bizdomain:80
## 客户端设置超时时间60s
[2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: keep-alive[r][n]"
[2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 >> "Keep-Alive: timeout=60[r][n]"
## 服务端设置超时时间5s
[2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 << "Keep-Alive: timeout=5, max=100[r][n]"
[2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 << "Connection: Keep-Alive[r][n]"
## 服务端设置生效,连接可以保持5s
[2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?) - Connection can be kept alive for 5000 MILLISECONDS
[2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection [id: 0][route: {}->http://bizdomain:80] can be kept alive for 5.0 seconds
[2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection released: [id: 0][route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200]
##Keep-Alive: timeout=60 非第一次请求
[2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200]
## 连接在上一次请求结束后5s失效
[2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.CPool:?) - Connection [id:2][route:{}->http://bizdomain:80][state:null] expired @ Wed Apr 26 14:10:05 GMT+08:00 2017
              
暂时禁止评论

微信扫一扫

易采站长站微信账号