O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Create an instance of jdk.incubator.http.HttpClient using its builder, jdk.incubator.http.HttpClient.Builder:
        HttpClient client = HttpClient.newBuilder().build();
  1. Create an instance of jdk.incubator.http.HttpRequest using its builder, jdk.incubator.http.HttpRequest.Builder. The requested URL should be provided as an instance of java.net.URI:
        HttpRequest request = HttpRequest                    .newBuilder(new URI("http://httpbin.org/get"))                    .GET()                    .version(HttpClient.Version.HTTP_1_1)                    .build();
  1. Send the HTTP request using the send API of jdk.incubator.http.HttpClient. This API takes an instance of jdk.incubator.http.HttpRequest and an implementation of jdk.incubator.http.HttpResponse.BodyHandler:
        HttpResponse<String> response = client ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required