Https Apache HTTPAsyncClient在响应之前关闭连接

Https Apache HTTPAsyncClient在响应之前关闭连接,https,maven-surefire-plugin,apache-httpasyncclient,Https,Maven Surefire Plugin,Apache Httpasyncclient,我正在使用Apache AsyncHTTPClient版本4.1.4(最新版本)向API发送GET请求。我似乎无法从服务器获得响应 这是我的请求日志 Jun 04, 2020 3:40:01 PM org.apache.http.impl.nio.client.MainClientExec generateRequest FINE: [exchange: 2] Connection route established Jun 04, 2020 3:40:01 PM org.apache.http

我正在使用Apache AsyncHTTPClient版本4.1.4(最新版本)向API发送GET请求。我似乎无法从服务器获得响应

这是我的请求日志

Jun 04, 2020 3:40:01 PM org.apache.http.impl.nio.client.MainClientExec generateRequest
FINE: [exchange: 2] Connection route established
Jun 04, 2020 3:40:01 PM org.apache.http.impl.nio.client.MainClientExec generateRequest
FINE: [exchange: 2] Attempt 1 to execute request
Jun 04, 2020 3:40:01 PM org.apache.http.impl.nio.client.MainClientExec generateRequest
FINE: [exchange: 2] Proxy auth state: UNCHALLENGED
Jun 04, 2020 3:40:01 PM org.apache.http.headers onRequestSubmitted
FINE: http-outgoing-1 >> GET /v1/contact?id=24 HTTP/1.1
Jun 04, 2020 3:40:01 PM org.apache.http.headers onRequestSubmitted
FINE: http-outgoing-1 >> Authorization: Bearer <token_removed>
Jun 04, 2020 3:40:01 PM org.apache.http.headers onRequestSubmitted
FINE: http-outgoing-1 >> Host: api.trial.ezyvet.com
Jun 04, 2020 3:40:01 PM org.apache.http.headers onRequestSubmitted
FINE: http-outgoing-1 >> Connection: Keep-Alive
Jun 04, 2020 3:40:01 PM org.apache.http.headers onRequestSubmitted
FINE: http-outgoing-1 >> User-Agent: Apache-HttpAsyncClient/4.1.4 (Java/11.0.6)
Jun 04, 2020 3:40:01 PM org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl setEvent
FINE: http-outgoing-1 192.168.1.2:52932<->52.27.52.37:443[ACTIVE][rw:][ACTIVE][rw][NEED_UNWRAP][0][0][437]: Event set [w]
Jun 04, 2020 3:40:01 PM org.apache.http.impl.nio.client.MainClientExec requestCompleted
FINE: [exchange: 2] Request completed
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 14.622 s
2020年6月4日下午3:40:01 org.apache.http.impl.nio.client.MainClientExec generateRequest
精细:[交换:2]已建立连接路由
2020年6月4日下午3:40:01 org.apache.http.impl.nio.client.MainClientExec generateRequest
罚款:[交易所:2]尝试1执行请求
2020年6月4日下午3:40:01 org.apache.http.impl.nio.client.MainClientExec generateRequest
罚款:[交易所:2]代理身份验证状态:未被质询
2020年6月4日下午3:40:01 org.apache.http.headers onRequestSubmitted
好:http-outgoing-1>>GET/v1/contact?id=24 http/1.1
2020年6月4日下午3:40:01 org.apache.http.headers onRequestSubmitted
精细:http-outgoing-1>>授权:承载
2020年6月4日下午3:40:01 org.apache.http.headers onRequestSubmitted
好:http-outgoing-1>>主机:api.trial.ezyvet.com
2020年6月4日下午3:40:01 org.apache.http.headers onRequestSubmitted
精细:http-outgoing-1>>连接:保持活动状态
2020年6月4日下午3:40:01 org.apache.http.headers onRequestSubmitted
精细:http-outgoing-1>>用户代理:Apache HttpAsyncClient/4.1.4(Java/11.0.6)
2020年6月4日下午3:40:01 org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl setEvent
精细:http-outgoing-1192.168.1.2:5293252.27.52.37:443[活动][rw:[活动][rw][NEED_UNWRAP][0][0][437]:事件集[w]
2020年6月4日下午3:40:01 org.apache.http.impl.nio.client.MainClientExec请求已完成
罚款:[交换:2]请求已完成
[信息]测试运行:1,失败:0,错误:0,跳过:0,运行时间:14.622秒
当我使用邮递员发送相同的请求时,我得到了成功的响应

这是我处理连接的代码

public void connect(HttpUriRequest请求,FutureCallback){
SSLContextBuilder SSLContextBuilder=新的SSLContextBuilder();
试一试{
loadTrustMaterial(null,新的TrustSelfSignedStrategy());
try(CloseableHttpAsyncClient=HttpAsyncClients.custom()
.setSSLContext(sslContextBuilder.build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build()){
client.start();
client.execute(请求、回调).get();
}
}捕获(例外e){
Logger.getGlobal().severe(“无法使用AsyncClient执行请求”);
}
}
我的调试器不会在回调的任何方法(已完成、失败、取消)中被触发。显然,由于某种原因,连接被终止

有人能解释一下这里发生了什么吗?
谢谢

我通过将代码更改为包含一个
倒计时锁存
而不是使用
客户机.execute(request,callback).get()
解决了这个问题

我将锁存传递给JSON反序列化程序读取
HttpResponse
的位置,并在一切完成后对其进行倒计时

这个答案帮助很大


我怀疑您可能在服务器响应完全传输之前关闭了客户端。发布您的应用程序代码。请检查以下要点:我正在使用IntelliJ社区版,我认为这是IDEs JUnit执行中的一个问题。当我添加一个
线程时,sleep(5000)连接启动后,运行正常。这与IDE无关。HttpAsyncClient API是事件驱动的,如果没有正确的事件同步,您可能会错误地使用它。线程睡眠也许可以解决眼前的问题,但它是错误的。