Close

Java 11 - Getting started with HTTP Client API

[Updated: Oct 6, 2018, Created: Sep 25, 2018]

Java 9 introduced HTTP Client as an incubating API (package jdk.incubator.http). Java 11 standardizes this API (package java.net.http).

This API provides non-blocking request and response semantics through CompletableFuture. The API supports both HTTP 1.1 and HTTP 2.

This API is the recommended alternative to URLConnection API.

In this getting started tutorial we will get familiar with the following classes provided in java.net.http package:

  • HttpRequest

    This class encapsulates HTTP request's URI, headers and body.

  • HttpRequest.Builder

    This builder helps us to prepare HttpRequest object. The instance of this builder is create by invoking HttpRequest.newBuilder(). All setter methods returns the same instance allowing the calls to be chained together. The HttpRequest.build() method returns a new HttpRequest instance.

  • HttpClient

    This class is used to send HTTP requests and to receive their responses. The instance of this class is obtained by invoking HttpClient.newHttpClient(). The method HttpClient.sendAsync() is used to send the given request asynchronously, it returns CompletableFuture<HttpResponse<T>>.

  • HttpResponse

    The instance of this class is returned as a result of sending an HttpRequest. We typically use one of its static nested interfaces to specify how we want to receive the response. For example HttpResponse.BodyHandler<T> is used to read the response body. It implements various useful handlers, such as handling the response body as a String, or streaming the response body to a file.

Example

package com.logicbig.example;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;

public class HttpClientExample {

  public static void main(String[] args) {
      //building request
      HttpRequest request = HttpRequest.newBuilder()
                                       .uri(URI.create("http://www.example.com/"))
                                       .GET()//used by default if we don't specify
                                       .build();
      //creating response body handler
      HttpResponse.BodyHandler<String> bodyHandler = HttpResponse.BodyHandlers.ofString();

      //sending request and receiving response via HttpClient
      HttpClient client = HttpClient.newHttpClient();
      CompletableFuture<HttpResponse<String>> future = client.sendAsync(request, bodyHandler);
      future.thenApply(HttpResponse::body)
            .thenAccept(System.out::println)
            .join();
  }
}

Output

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

   ...............
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is established to be used for illustrative examples in documents. You may use this
    domain in examples without prior coordination or asking for permission.</p>
......................

Example Project

Dependencies and Technologies Used:

  • JDK 11
Getting started with HttpClient API Select All Download
  • java-11-http-client
    • src
      • com
        • logicbig
          • example
            • HttpClientExample.java

    See Also