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".
- When I make a
GETrequest fromclientthen exactly oneTCPconnection is created for each request. Incoming headers don't have any information aboutkeep-alive - When I try to set
keep-aliveheader directly I got the exception.
HttpRequest req = HttpRequest.newBuilder()
.setHeader("Connection", "Keep-Alive")
.uri(uri)
.build();
- When I make a
GETrequest from a browser(safari)then the browser addskeep-aliveheaders to each request and only oneTCPconnection is created for multiply requests (as expected). - When I set version HTTP/2 and make the request from the client then only one
TCPconnection 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?
java clientinstead ofapache 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.