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?

4
  • " 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. Commented Dec 2, 2021 at 16:50
  • @SteffenUllrich thank you! I've never known about it. You are absolutely right. Explanation on wikipedia. Do you have any idea why then it creates TCP connections for each request (and why a browser doesn't do the same)? Commented Dec 3, 2021 at 6: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?. 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. Commented Dec 3, 2021 at 7: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. Commented Dec 3, 2021 at 9:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.