Reputation: 8958
HTTP connection can be kept alive with a parameter setting,
Keep-Alive: timeout=15, max=100
Is it possible to keep the connection alive indefinitely to a server endpoint? If so, how?
Upvotes: 1
Views: 6232
Reputation: 4132
You can execute a fake "ping" request in loop in order to keep connection alive as long as possible, if there are no real requests going on for some time. However this puts unnecessary load to the server and performance gain in your app will be minimal.
Upvotes: 0
Reputation: 1936
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.
Upvotes: 2