英文:
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
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论