0

I am sending a GET request to an API using the Apache AsyncHTTPClient version 4.1.4 (latest). I can't seem to get a response from the server.

This is my request log.

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

When I send the same request using Postman, I get a successful response.

This is my code to handle the connection.

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.");
    }
}

My debugger won't get triggered in any of the callback's methods (completed, failed, cancelled). Apparently the connection gets terminated for some reason.

Can someone explain what is happening here? Thanks

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Saminda Peramuna
  • 735
  • 9
  • 22
  • 1
    I suspect that you might be shutting down the client before the server response is fully transmitted. Post your application code. – ok2c Jun 04 '20 at 10:34
  • Please check this gist: https://gist.github.com/samindaperamuna/edadce4029e9060755c1e1928553a177 – Saminda Peramuna Jun 04 '20 at 10:40
  • I am using IntelliJ Community Edition and I think this is an issue withe the IDEs JUnit execution. When I added a `Thread.sleep(5000);` after the connection starts, it runs fine. – Saminda Peramuna Jun 04 '20 at 11:38
  • 1
    This has to do nothing with the IDE. HttpAsyncClient API is event driven and you are likely using it incorrectly without proper event synchronization. Thread#sleep might solve the immediate problem but it is wrong. – ok2c Jun 04 '20 at 11:56

1 Answers1

1

I managed to solve this issue by changing the code to include a CountDownLatch instead of using client.execute(request, callback).get().

I passed down the latch to where the HttpResponse is read by JSON deserializer and count it down after everything completes.

This answer helped a lot.

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

Saminda Peramuna
  • 735
  • 9
  • 22