我正在向使用Apache 4.1.4 (最新版本)的AsyncHTTPClient发送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当我使用Postman发送相同的请求时,我得到了一个成功的响应。
这是我处理连接的代码。
public void connect(HttpUriRequest request, FutureCallback<HttpResponse> callback) {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
try {
sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
try (CloseableHttpAsyncClient client = HttpAsyncClients.custom()
.setSSLContext(sslContextBuilder.build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build()) {
client.start();
client.execute(request, callback).get();
}
} catch (Exception e) {
Logger.getGlobal().severe("Couldn't execute the request using the AsyncClient.");
}
}我的调试器不会在回调的任何方法中被触发(完成、失败、取消)。显然,由于某种原因,连接被终止了。
有人能解释一下这里发生了什么吗?谢谢
发布于 2020-06-05 07:10:04
通过更改代码以包含CountDownLatch而不是使用client.execute(request, callback).get(),我设法解决了这个问题。
我将闩锁传递到JSON反序列化器读取HttpResponse的位置,并在一切完成后对其进行倒计时。
这个答案很有帮助。
https://stackoverflow.com/questions/62192304
复制相似问题