Java 9 : HTTP GET request example
Using the JDK 9 HTTP Client API to make a GET request to the URL
Package
Package
- jdk.incubator.http.HttpClient
- jdk.incubator.http.HttpRequest
- java.net.URI
- HttpClient client = HttpClient.newBuilder().build();
- HttpRequest request = HttpRequest.newBuilder(new URI("http://java.com")).GET().version(HttpClient.Version.HTTP_1_1).build();
- HttpResponse<String> response = client.send(request,HttpResponse.BodyHandler.asString());
- System.out.println("Status code: " + response.statusCode());
- System.out.println("Response Body: " + response.body());
Print the status code and the response body.
Comments
Post a Comment