我正在开发一个编写客户端-服务器应用程序的项目。我是网络概念的新手,我正在使用 HttpClient 5.x 向我的服务器发出请求。我想完全断开客户端和服务器之间的 TCP 连接。
当我使用 client.close() 时,它仍然允许服务器发送响应。我知道 close 方法不允许客户端发出任何新请求,但服务器将能够向同一连接发送响应。即使服务器正在后台处理我的请求,我如何也可以强制客户端和服务器都断开连接?甚至可能吗?
我的代码:
CloseableHttpClient httpClient = HttpClients.custom().build();
HttpGet request = new HttpGet("https://127.0.0.1:8001/get-values");
Thread sendRequest = new Thread(() -> {
try {
httpClient.execute(request);
} catch (IOException e) {
e.printStackTrace();
}
});
Thread closeConnection = new Thread(() -> {
httpClient.close(CloseMode.IMMEDIATE);
});
sendRequest.start();
closeConnection.start();
sendRequest.join();
closeConnection.join();