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 a default instance of org.http.client.HttpClient using its org.apache.http.impl.client.HttpClients factory:
        CloseableHttpClient client = HttpClients.createDefault();
  1. Create an instance of org.apache.http.client.methods.HttpGet along with the required URL. This represents both the HTTP method type and the requested URL:
        HttpGet request = new HttpGet("http://httpbin.org/get");
  1. Execute the HTTP request using the HttpClient instance to obtain an instance of CloseableHttpResponse:
        CloseableHttpResponse response = client.execute(request);

The CloseableHttpResponse instance returned after executing the HTTP request can be used to obtain details such as the response status code and other contents of the response embedded ...

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