使用apache httpclient可以永远保持http连接keep-alive吗?

时间:2015-07-28 17:41:37

标签: java apache-httpclient-4.x apache-httpcomponents

使用参数设置

可以保持HTTP连接

Keep-Alive:超时= 15,最大= 100

是否可以将连接无限期地保持在服务器端点上?如果是这样,怎么样?

2 个答案:

答案 0 :(得分:1)

From what you've said, I think this page will give you the answers you need. The most pertinent quote on there:

If the Keep-Alive header is not present in the response, HttpClient assumes the connection can be kept alive indefinitely. However, many HTTP servers in general use are configured to drop persistent connections after a certain period of inactivity in order to conserve system resources, quite often without informing the client.

So, basically, it depends on how cooperative the server is. Ultimately, though, keep in mind that a keep-alive value is a suggestion to the server, and you can't guarantee that it will be honored. This can be due to servers trying to reclaim limited resources from inactive connections, or problems in the underlying TCP connection, or just bad programming, so it's a good idea to have a strategy in place to reconnect on a failure.

答案 1 :(得分:0)

如果没有真正的请求持续一段时间,您可以在循环中执行假的“ping”请求,以便尽可能长时间地保持连接活动。但是,这会给服务器带来不必要的负担,并且应用程序的性能提升也会很小。