其他情况测试
  客户端设置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[ ][ ]"
  ## 服务端返回的也是短连接
  [2017-04-26 13:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Connection: close[ ][ ]"
  ##请求完之后,关闭连接
  [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[ ][ ]"
  [2017-04-26 10:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Keep-Alive: timeout=60[ ][ ]"
  ## 服务端设置超时时间5s
  [2017-04-26 10:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Keep-Alive: timeout=5, max=100[ ][ ]"
  [2017-04-26 10:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Connection: Keep-Alive[ ][ ]"
  ## 服务端设置生效,连接可以保持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
  客户端设置失效时间,服务端设置不失效
  ## 客户端设置30s超时
  [2017-04-26 17:45:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: keep-alive[ ][ ]"
  [2017-04-26 17:45:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Keep-Alive: timeout=30[ ][ ]"
  ## 服务端设置连接
  [2017-04-26 17:45:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Connection: keep-alive[ ][ ]"
  ## 连接将一直保持
  [2017-04-26 17:45:00 DEBUG]  (org.apache.http.impl.execchain.MainClientExec:?) - Connection can be kept alive indefinitely
  综上,http连接保持时间是由服务端的消息头connection字段和keep-alive字段定的。
  在上面前两种情况,请求的是同一个服务端,那么为什么一个返回的是短连接,一个返回的是长连接呢?这里转一下 这篇文章的解释:
  不论request还是response的header中包含了值为close的connection,都表明当前正在使用的tcp链接在请求处理完毕后会被断掉。以后client再进行新的请求时必须创建新的tcp链接了。 HTTP Connection的 close设置允许客户端或服务器中任何一方关闭底层的连接,双方都会要求在处理请求后关闭它们的TCP连接。
  补充
  · TCP长短连接
  在网上搜资料的时候,看到很多“HTTP协议的长连接和短连接,实质上是TCP协议的长连接和短连接”。 HTTP和TCP是不同两层的东西,它们怎么会是一样的呢?HTTP是请求/响应模式的,是说我们发一个请求一定要有一个回应。直观的是,浏览器上发请求,得不到响应会一直转圈圈。 而TCP并不是一定要有响应。大家以前使用socket模拟一个IM聊天,A跟B打完招呼,完全可以不用等待B的回应,自己关掉连接的。
  · TCP keep-alive
  另外还有HTTP协议的keep-alive和TCP的keep-alive含义是有差别的。HTTP的keep-alive是为了维持连接,以便复用连接。通过使用keep-alive机制,可以减少tcp连接建立次数,也意味着可以减少TIME_WAIT状态连接,以此提高性能和提高httpd服务器的吞吐率(更少的tcp连接意味着更少的系统内核调用,socket的accept()和close()调用)。但是,长时间的tcp连接容易导致系统资源无效占用。配置不当的keep-alive,有时比重复利用连接带来的损失还更大。
  而tcp keep-alive是TCP的一种检测TCP连接状况的机制,涉及到三个参数tcp_keepalive_time, tcp_keepalive_intvl, tcp_keepalive_probes。
  当网络两端建立了TCP连接之后,闲置(双方没有任何数据流往来)了tcp_keepalive_time后,服务器内核会尝试向客户端发送侦测包,来判断TCP连接状况(有可能客户端崩溃、强制关闭了应用、主机不可达等等)。如果没有收到对方的回答(ack包),则会在 tcp_keepalive_intvl后再次尝试发送侦测包,直到收到对方的ack。如果一直没有收到对方的ack,一共会尝试 tcp_keepalive_probes次。如果尝试tcp_keepalive_probes,依然没有收到对方的ack包,则会丢弃该TCP连接。TCP连接默认闲置时间是2小时,一般设置为30分钟足够了。