<分区>
我一直在使用 try-with-resources 搜索 CloseableHttpClient
的完整示例。我很困惑,如果关闭 CloseableHttpClient
也会关闭将在我调用 httpclient.execute(post)
时创建的 CloseableHttpResponse
对象。我是否也需要将 CloseableHttpResponse
包装在 try-with-resources 中?
例子:
try(CloseableHttpClient httpclient = HttpClients.custom().build()) {
HttpPost post = new HttpPost(url);
CloseableHttpResponse res = httpclient.execute(post);
// do something with res
} catch (Throwable e) {
// do something with error
}