forked from wesleyegberto/java-new-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetRequestAsyncTest.java
More file actions
20 lines (18 loc) · 744 Bytes
/
GetRequestAsyncTest.java
File metadata and controls
20 lines (18 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class GetRequestAsyncTest {
public static void main(String[] args) throws InterruptedException {
HttpRequest request = HttpBinRequestBuilder.createGetRequest();
HttpClient httpClient = HttpClientBuilderTest.createHttpClient();
// HttpClient#sendAsync is non-blocking and returns a CompletableFuture
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(body -> System.out.println("Async Response: " + body))
.exceptionally(ex -> {
System.out.println("Exception: " + ex.getLocalizedMessage());
return null;
})
.join(); // let's wait
}
}