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
GET
request fromclient
then exactly oneTCP
connection is created for each request. Incoming headers don't have any information aboutkeep-alive
- When I try to set
keep-alive
header directly I got the exception.
HttpRequest req = HttpRequest.newBuilder()
.setHeader("Connection", "Keep-Alive")
.uri(uri)
.build();
- When I make a
GET
request from a browser(safari)
then the browser addskeep-alive
headers to each request and only oneTCP
connection is created for multiply requests (as expected). - 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?