March 2019
Intermediate to advanced
242 pages
6h 21m
English
To receive a response asynchronously, you can use the HttpClient sendAsync() method. This request will immediately return CompletableFuture. You can call the get() method on CompletableFuture to retrieve the response.
Let's modify the example used in the preceding section to receive the response (HTML text) in a file in an asynchronous manner:
class AsyncGetHTMLToFile {
public static void main(String args[]) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://docs.oracle.com/en /java/javase/11/docs/api/java.net.http/java/net /http/HttpClient.html")) .build(); CompletableFuture<Path> response = client.sendAsync(request, ...Read now
Unlock full access