//Create an instance of HttpClient using its builder. HttpClient httpClient = HttpClient.newBuilder() .version(HttpClient.Version.HTTP_2) .build(); //Create an instance of HttpRequest using its builder. HttpRequest req = HttpRequest.newBuilder(URI.create("https://www.google.com")) .GET() .build(); /* Use the httpClient.sendAsync() method and encapsulate the response in the CompletableFuture. */ CompletableFuture<HttpResponse> resFuture = httpClient.sendAsync(req, HttpResponse.BodyHandlers.ofString()); //Use the resFuture.thenAccept to wait for the async response. resFuture.thenAccept(res -> System.out.println(res.version())); //Wait for the resFuture to to complete. resFuture.join();
Leave a Reply