2018-10-28

Java 9 : HTTP GET request example

Using the JDK 9 HTTP Client API to make a GET request to the URL

Package

  • jdk.incubator.http.HttpClient
  • jdk.incubator.http.HttpRequest
  • java.net.URI
  1. HttpClient client = HttpClient.newBuilder().build();
  2. HttpRequest request = HttpRequest.newBuilder(new URI("http://java.com")).GET().version(HttpClient.Version.HTTP_1_1).build();
  3. HttpResponse<String> response = client.send(request,HttpResponse.BodyHandler.asString());
  4. System.out.println("Status code: " + response.statusCode());
  5. System.out.println("Response Body: " + response.body());


Print the status code and the response body.


No comments:

Post a Comment