gpt4 book ai didi

java - CloseableHttpAsyncClient 不发送 http 请求

转载 作者:行者123 更新时间:2023-12-01 19:10:15 26 4
gpt4 key购买 nike

我想使用 CloseableHttpAsyncClient 以异步模式发送 http 请求。但请求没有发出去。当启用注释 HttpResponse response = future.get() 时。有用。但我想知道为什么我需要 future.get() 即使我不关心响应。代码在这里

public class CloseableHttpAsyncClientTest {

@Test
public void whenUseHttpAsyncClient_thenCorrect() throws Exception {
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
HttpDelete request = new HttpDelete("http://www.bing.com");
Future<HttpResponse> future = client.execute(request, null);
// The delete request will send out if we remove comment here. We just want to send out delete http
// request but not care about the response
// HttpResponse response = future.get();
client.close();
}
}

控制台是这样的

20:28:48.930 [main] DEBUG org.apache.http.impl.nio.client.MainClientExec - [exchange: 1] start execution
20:28:48.941 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
20:28:48.950 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
20:28:48.951 [main] DEBUG org.apache.http.impl.nio.client.InternalHttpAsyncClient - [exchange: 1] Request connection for {}->http://www.bing.com:80
20:28:48.953 [main] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Connection request: [route: {}->http://www.bing.com:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
20:28:48.976 [main] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Connection manager is shutting down
20:28:49.003 [main] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Connection manager shut down

启用评论后。像这样。它有效。

public class CloseableHttpAsyncClientTest {

@Test
public void whenUseHttpAsyncClient_thenCorrect() throws Exception {
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
HttpDelete request = new HttpDelete("http://www.bing.com");
Future<HttpResponse> future = client.execute(request, null);
// The delete request will send out if we remove comment here. We just want to send out delete http
// request but not care about the response
HttpResponse response = future.get();
client.close();
}
}

从日志中我们可以看到。 http删除已发出。

20:39:15.998 [main] DEBUG org.apache.http.impl.nio.client.MainClientExec - [exchange: 1] start execution
......
......

20:39:16.137 [I/O dispatcher 1] DEBUG org.apache.http.headers - http-outgoing-0 >> DELETE / HTTP/1.1
20:39:16.137 [I/O dispatcher 1] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: www.bing.com
......
......


20:39:16.142 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 >> "DELETE / HTTP/1.1[\r][\n]"
20:39:16.142 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: www.bing.com[\r][\n]"
20:39:16.142 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
20:39:16.142 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpAsyncClient/4.1.4 (Java/1.8.0_202)[\r][\n]"
20:39:16.143 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
20:39:16.143 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalIODispatch - http-outgoing-0 [ACTIVE] Request ready
20:39:16.143 [I/O dispatcher 1] DEBUG
......

20:39:16.209 [main] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Connection manager shut down

最佳答案

Future<HttpResponse> future = client.execute(request, null);

client.execute将以异步方式发出http请求。将需要另一个线程来完成http请求。

HttpResponse response = future.get(); 

future.get() 将阻塞主线程,直到 client.execute 完成并将响应填充到 future 中。如果没有这个,客户端将在 http 请求发送之前关闭。因为http请求是由一个线程发出的,但是客户端关闭是由主线程关闭的。因此,主线程需要被阻塞,直到 future.get() 被另一个线程获取结果。

关于java - CloseableHttpAsyncClient 不发送 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59488720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com