5

I would like to ask if this code after execution is auto-closing the connection. Also if it fail and crash, is it still going to close connection?

HttpClient.newHttpClient().send(
    HttpRequest.newBuilder()
        .uri(URI.create("url_website"))
        .timeout(Duration.ofSeconds(5))
        .GET()
        .build(),
    HttpResponse.BodyHandlers.ofString())
.body()
Robert
  • 7,394
  • 40
  • 45
  • 64
  • 4
    You can take a tcpdump and find out. It’ll also depend on the protocol version, and presence of `Connection` header. – Abhijit Sarkar Oct 14 '20 at 22:24
  • 1
    Probably not, the [documentation](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html) doesn't say it's [`AutoCloseable`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/AutoCloseable.html). – Robert Oct 14 '20 at 22:31

1 Answers1

2

The HttpClient uses a connection pool (one for HTTP/1.1, one for HTTP/2) so connections will be pooled - and therefore not closed immediately unless requested by the server (HTTP/1.1: connection: close).

daniel
  • 2,665
  • 1
  • 8
  • 18