HttpClient 中的意外连接关闭

Unexpected connection close in HttpClient

我在 Web 应用程序中遇到 HttpClient(版本 4.5.2)问题,我的意思是,以多线程方式。在正常情况下,当连接请求到达时,从池中租用一个连接,然后使用并最终再次释放回池中,以在以后的请求中再次使用,作为 id 673890 状态连接日志的以下部分。

15 Feb 2017 018:25:54:115 p-1-thread-121 DEBUG PoolingHttpClientConnectionManager:249 - Connection request: [route: {}->http://127.0.0.1:8080][total kept alive: 51; route allocated: 4 of 100; total allocated: 92 of 500]
15 Feb 2017 018:25:54:116 p-1-thread-121 DEBUG PoolingHttpClientConnectionManager:282 - Connection leased: [id: 673890][route: {}->http://127.0.0.1:8080][total kept alive: 51; route allocated: 4 of 100; total allocated: 92 of 500]
15 Feb 2017 018:25:54:116 p-1-thread-121 DEBUG DefaultManagedHttpClientConnection:90 - http-outgoing-673890: set socket timeout to 9000
15 Feb 2017 018:25:54:120 p-1-thread-121 DEBUG PoolingHttpClientConnectionManager:314 - Connection [id: 673890][route: {}->http://127.0.0.1:8080] can be kept alive for 10.0 seconds
15 Feb 2017 018:25:54:121 p-1-thread-121 DEBUG PoolingHttpClientConnectionManager:320 - Connection released: [id: 673890][route: {}->http://127.0.0.1:8080][total kept alive: 55; route allocated: 4 of 100; total allocated: 92 of 500]

以我上面提到的正常方式多次使用提到的连接 (id 673890) 后,我注意到代码中发生了以下情况:

15 Feb 2017 018:25:54:130 p-1-thread-126 DEBUG PoolingHttpClientConnectionManager:249 - Connection request: [route: {}->http://127.0.0.1:8080][total kept alive: 55; route allocated: 4 of 100; total allocated: 92 of 500]
15 Feb 2017 018:25:54:130 p-1-thread-126 DEBUG PoolingHttpClientConnectionManager:282 - Connection leased: [id: 673890][route: {}->http://127.0.0.1:8080][total kept alive: 54; route allocated: 4 of 100; total allocated: 92 of 500]
15 Feb 2017 018:25:54:131 p-1-thread-126 DEBUG DefaultManagedHttpClientConnection:90 - http-outgoing-673890: set socket timeout to 9000
15 Feb 2017 018:25:54:133 p-1-thread-126 DEBUG DefaultManagedHttpClientConnection:81 - http-outgoing-673890: Close connection
15 Feb 2017 018:25:54:133 p-1-thread-126 DEBUG PoolingHttpClientConnectionManager:320 - Connection released: [id: 673890][route: {}->http://127.0.0.1:8080][total kept alive: 55; route allocated: 3 of 100; total allocated: 91 of 500]

日志显示连接被请求、租用、使用、关闭然后释放回池中。所以,我的问题是为什么连接被关闭?以及为什么关闭后释放到池中?

我知道连接可能会被服务器关闭,但那是另外一种情况。在那种情况下,连接是从池中租用的,确定为过时连接,因此会建立并使用新连接,但我上面提供的日志显示了不同的行为。

我知道在 HttpClient 中关闭连接的两个原因。首先,因空闲而关闭,因为它们的 KeepAliveTime 已过期。其次,被服务器关闭,这使得池中的连接失效。还有其他原因导致连接关闭吗?

根据 Oleg Kalnichevski 在 HttpClient 邮件列表中的回复,以及我所做的检查,原来问题是因为 'Connection: close' header 由对方发送。可能导致相同情况的另一个原因是使用 HTTP/1.0 non-persistent 连接。