From HTTP 1.1, support is enabled by default. You need to close the connection if you do not want it to be used explicitly when working with HTTP 1.1.
For 1.0, the header is what you set for this "Connection: Keep-alive" This only tells the server that you want to reuse the connection. Under stress or for other reasons, the server may act differently, as described below.
In most cases, most of the answers here are correct, where you add the keep alive header and it works well. The following explanation applies to the scenarios where you did this, but this still does not work.
Server side issues
Usually the answer will focus on tuning when the server will behave normally, but this is not entirely true. Servers (e.g. Rudra ) are built to behave differently in different situations. Keep-alive comes with several requests that the server would serve you before you cancel your connection, which means that you must also provide services to other users, and in case of high load some servers may resort to reducing the number of keep-alive requests for every new connection.
There is also a timeout from the last request received, which will ultimately lead to disconnection if no further requests are made during this time. Few modern servers can change this based on the ability they currently have, or lower it to 0 in panic conditions, making it meaningless alive. Therefore, if the server with which you are trying to establish a connection passes through any of these (racing, panic) conditions, it may refuse your request.
Client side issues
For documentation purposes. From hc.apache.org :
HttpClient always does its best to reuse connections. persistence connection is enabled by default and does not require configuration. Under in some situations, this can lead to leakage of connections and, consequently, to loss of resources. The easiest way to disable persistence of connections is to provide or extend the connection manager, which forcibly closes connections after release in the releaseConnection method.
HttpClient offers these (read: trivial) things Out Of The Box. But there are still other things that Apache offers that you can add to improve its performance.
ConnectionManager (s), for example, can be configured for HttpClient.
Thus, all that can block the persistence / persistence of connections is a connection manager that you can use (this is not true in your case, but it may be true in several other cases). This may be a completely unknown / abstract fact for you if you get a Client object to make calls from some API. An example of how this can be configured is given below (from the Apache Connection Management documentation)
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // Increase max total connection to 200 cm.setMaxTotal(200); // Increase default max connection per route to 20 cm.setDefaultMaxPerRoute(20); // Increase max connections for localhost:80 to 50 HttpHost localhost = new HttpHost("locahost", 80); cm.setMaxPerRoute(new HttpRoute(localhost), 50); CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(cm) .build();
(see Apache Connection Management documentation for more details)
If you run into this problem, try without CM or create your own HttpClient object. Also, you do not need to use CM for multiple connections. Built-in CM is fair enough. If you see a performance loss, you can write your own connection manager.
In your case, however, your server is not very supportive; it may not respect Keep-alive at all, even if you have these headers and what not. You will need to check the timeout header in response by sending keep-alive in a new request to the server to establish that the server is a complaint.