HttpClient关闭连接

huangapple go评论82阅读模式
英文:

HttpClient close connection

问题

我想询问一下,这段代码在执行后是否会自动关闭连接。
另外,如果代码执行失败并崩溃,它是否仍然会关闭连接?

HttpClient.newHttpClient().send(
    HttpRequest.newBuilder()
        .uri(URI.create("url_website"))
        .timeout(Duration.ofSeconds(5))
        .GET()
        .build(),
    HttpResponse.BodyHandlers.ofString())
.body()
英文:

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()

答案1

得分: 2

HttpClient使用连接池(对于HTTP/1.1和HTTP/2分别使用一个),因此连接将会被池化,除非服务器要求关闭(HTTP/1.1: connection: close),否则连接不会立即关闭。

英文:

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).

huangapple
  • 本文由 发表于 2020年10月15日 06:16:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64362248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定