首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CloseableHttpAsyncClient不发送http请求

CloseableHttpAsyncClient不发送http请求
EN

Stack Overflow用户
提问于 2019-12-26 12:48:39
回答 1查看 254关注 0票数 1

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

代码语言:javascript
复制
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();
    }
}

像这样的控制台

代码语言:javascript
复制
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

在我启用评论之后。就像这样。它起作用了。

代码语言:javascript
复制
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删除。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-28 01:26:01

代码语言:javascript
复制
Future<HttpResponse> future = client.execute(request, null);

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

代码语言:javascript
复制
HttpResponse response = future.get(); 

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59488720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档