0

I configured java.net.http.HttpClient as shown below:

HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();

Also, I have a simple Spring Boot (Tomcat) HTTP server, which is running on the 8080 port. For each request, I check incoming headers in a controller and the number of TCP connections using the next command: lsof -i -P | grep "TCP" | grep "8080".

  1. When I make a GET request from client then exactly one TCP connection is created for each request. Incoming headers don't have any information about keep-alive
  2. When I try to set keep-alive header directly I got the exception.
HttpRequest req = HttpRequest.newBuilder()
                             .setHeader("Connection", "Keep-Alive")
                             .uri(uri)
                             .build();
  1. When I make a GET request from a browser (safari) then the browser adds keep-alive headers to each request and only one TCP connection is created for multiply requests (as expected).
  2. When I set version HTTP/2 and make the request from the client then only one TCP connection creates for all requests (as expected):
 HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).build();

As described here - both HTTP/1.1 and HTTP/2 have keep-alive property which is enabled by default, but as you can see from the examples above it doesn't work for HTTP/1.1 in my case. Does anyone know how to configure HttpClient properly? Or maybe, I'm doing something wrong?

Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34
  • *" Incoming headers don't have any information about keep-alive"* - for HTTP/1.1 the default is keep-alive, unless explicitly specified otherwise. Thus a HTTP/1.1 response with no Connection header is equivalent to having a Connection header of keep-alive. – Steffen Ullrich Dec 02 '21 at 16:50
  • @SteffenUllrich thank you! I've never known about it. You are absolutely right. Explanation on [wikipedia](https://en.wikipedia.org/wiki/HTTP_persistent_connection). Do you have any idea why then it creates TCP connections for each request (and why a browser doesn't do the same)? – Volodya Lombrozo Dec 03 '21 at 06:49
  • Hard to tell due to missing code for the actual request handling. But have a look at [How to get persistent HttpConnection with Apache HttpClient?](https://stackoverflow.com/questions/46053067/how-to-get-persistent-httpconnection-with-apache-httpclient). Note that with HTTP/2 multiple requests are sent in parallel over the same TCP connection while with HTTP/1.1 only one after the other. Thus you need to make sure to end the previous one properly before starting the next - otherwise it will need to create a new TCP connection. – Steffen Ullrich Dec 03 '21 at 07:00
  • Thank you, I'm working with `java client` instead of `apache client`, and the StackOverflow question (which you suggest) actually doesn't fit my problem. Also, I've disabled parallel run, as you suggested, but the client with version HTTP_1_1 still creates a new TCP connection for each request. – Volodya Lombrozo Dec 03 '21 at 09:53

0 Answers0