基础连接在每个请求时被打开、每个响应接收和实体被执行(实体被读)时被关闭。请看下面的例子:
Example 5.5. Closing connections
final WebTarget target = ... some web target
Response response = target.path("resource").request().get();
System.out.println("Connection is still open.");
System.out.println("string response: " + response.readEntity(String.class));
System.out.println("Now the connection is closed.");
如果你不读实体,那么你需要通过 response.close() 手动关闭响应响应。如果实体读入 InputStream(通过 response.readEntity(InputStream.class)),连接保持打开状态,直到你完成读取 InputStream。在这种情况下,该 InputStream 或响应应该在读取 InputStream 完成后手动关闭。