Triggering asynchronous GET request is a three-step job, as follows:
- Create a new HttpClient object (java.net.http.HttpClient):
HttpClient client = HttpClient.newHttpClient();
- Build an HttpRequest object (java.net.http.HttpRequest) and specify the request (by default, this is a GET request):
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://reqres.in/api/users/2")) .build();
For setting the URI, we can call the HttpRequest.newBuilder(URI) constructor or call the uri(URI) method on the Builder instance (like we did previously).
- Trigger the request and wait for the response (java.net.http.HttpResponse). Being a synchronous request, the application will block until the response ...