【问题标题】:Apache HTTPAsyncClient closes connection before responseApache HTTPAsyncClient 在响应之前关闭连接
【发布时间】:2020-09-23 07:12:38
【问题描述】:

我正在使用 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

当我使用 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.");
    }
}

我的调试器不会在任何回调方法(完成、失败、取消)中被触发。显然连接由于某种原因被终止了。

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

【问题讨论】:

  • 我怀疑您可能在服务器响应完全传输之前关闭了客户端。发布您的应用程序代码。
  • 请检查这个要点:gist.github.com/samindaperamuna/…
  • 我正在使用 IntelliJ 社区版,我认为这是 IDE JUnit 执行的问题。当我在连接开始后添加Thread.sleep(5000); 时,它运行良好。
  • 这与 IDE 无关。 HttpAsyncClient API 是事件驱动的,如果没有正确的事件同步,您可能会错误地使用它。 Thread#sleep 可能会解决眼前的问题,但它是错误的。

标签: https maven-surefire-plugin apache-httpasyncclient


【解决方案1】:

我设法通过将代码更改为包含CountDownLatch 而不是使用client.execute(request, callback).get() 来解决此问题。

我将闩锁传递到 JSON 反序列化器读取 HttpResponse 的位置,并在一切完成后倒计时。

这个答案很有帮助。

https://stackoverflow.com/a/49359725/6663366

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2016-09-09
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多