1

I call below code within my application. The first request is always working fine. My issue is that every following request is not sent, it runs into timeout, when I specify a timeout value. Otherwise it seems to wait endlessly. It seems the first request blocks the connection for every following attempt. How can I ensure the connection is properly released again? Maybe some headers? Maybe some properties (defaults are used for http https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/doc-files/net-properties.html)?

HttpRequest request = HttpRequest.newBuilder()
                .version(HttpClient.Version.HTTP_1_1)
                .GET()
                .uri(URI.create(url))
                .build();
try {
  HttpResponse<Path> response = client.send(request, HttpResponse.BodyHandlers.ofFile(Paths.get(outfile)));
} catch (IOException | InterruptedException e) {
  // ...
}

used: java.net.http.HttpClient (AdoptOpenJDK 11.0.7_10)

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
MoxxiManagarm
  • 8,735
  • 3
  • 14
  • 43
  • 1
    I would recommend to investigate server side as well, as per my knowledge i don't see it as connection problem – Ryuzaki L Jun 23 '20 at 13:19
  • None-download requests work fine from the same server, only this download creates issues – MoxxiManagarm Jun 23 '20 at 13:26
  • what do you mean by subsequent requests are blocking ? can you show the code how are you calling this method multiple times ? – Ryuzaki L Jun 23 '20 at 14:11
  • As a start to help debugging I'd suggest logging the request/response made by the stack: `-Djdk.httpclient.HttpClient.log=errors,requests,headers` – daniel Jun 24 '20 at 09:19

1 Answers1

0

The send method is a Sync method, so the request get block until you get a response, in this case maybe you are not getting the response.

send(HttpRequest, BodyHandler) blocks until the request has been sent and the response has been received.

Try using the Async method

sendAsync(HttpRequest, BodyHandler) sends the request and receives the response asynchronously. The sendAsync method returns immediately with a CompletableFuture. The CompletableFuture completes when the response becomes available. The returned CompletableFuture can be combined in different ways to declare dependencies among several asynchronous tasks.

Example of an async request (taken from the apidoc):

 HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://example.com/"))
        .timeout(Duration.ofMinutes(2))
        .header("Content-Type", "application/json")
        .POST(BodyPublishers.ofFile(Paths.get("file.json")))
        .build();
   client.sendAsync(request, BodyHandlers.ofString())
        .thenApply(HttpResponse::body)
        .thenAccept(System.out::println);
ARandomGuy
  • 21
  • 2
  • As said the first request is working fine, the second request doesn't even go out. I also tried the to change the order, same behevior - the first sent request works, the second is blocked. But I am going to play around with sendAsync, maybe it helps, thank you – MoxxiManagarm Jun 23 '20 at 13:29