Introduction to HttpClient

software engineer looking at an ipad
Reading Time: 2 minutes

Introduction

HttpClient is used to send the request and receive a response. The HttpClient library was introduced in Java 11, before that developers had to use some third-party libraries such as Apache Http Client, OkHttp, or the legacy class HttpUrlConnection. It replaces the HttpUrlConnection class in the JDK since the early version of java.

Consider a browser as a client and an application executing on a computer hosting a website as a server.

The client first initiates an HTTP request message to the server. The server then collects requested resources such as HTML files or other content and responds to the client. If the asked resources are not found on the server it will send an error to the client.

Important features of HttpClient

  • It supports both HTTP/1.1 and HTTP/2 and WebSocket.
  • It can be used to send asynchronous requests to not wait for the response and start processing it only when it is received.
  • It is possible to access the HTTP headers of the response.
  • supports using cookies.

Core Classes

HttpRequest

It is an object that defines the request we want to send. New instances can be created using HttpRequest.Builder. The request builder can be used to set request URI, and request methods such as GET, POST, PUT, request body, timeout, and request headers. We can get it by calling HttpRequest.newBuilder(). The builder class provides plenty of methods that we can operate to configure our request.

HttpClient

HttpClient is used to send the request and receive a response. The response is obtained in the form of HttpResponse objects. After transmitting a request via the client, the received response bodies can be handled utilizing the following BodyHandlers:

  1. statusCode():- It returns the status code (type int) for a response.

2. body():- It returns a body for a response.

HttpResponse

The HTTP response object illustrates the response from the server for the provided request. On a high-level HttpResponse provide the following essential information.

  • statusCode():– It returns the status code for this response.
  • body():– It returns the response body.
  • headers() :– Response headers.
  • URI ():– It returns the URI that the response was received from.
  • version():– It returns the HTTP protocol version used for this response.

Steps to use HttpClient

Below are steps to use an HttpClient.

1) Build an HttpClient instance using HttpClient.newBuilder() instance.

HttpClient client = HttpClient.newHttpClient();

2) Build an HttpRequest instance using HttpRequest.newBuilder() instance.

HttpRequest request = HttpRequest.newBuilder()

                      .uri(URI.create("https://api.publicapis.org/entries"))

                      .GET() 

                      .build();

3) Create a request using the HTTP client.send() and get a response object

HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());

Conclusion

In this blog, we have demonstrated the basic concepts of HTTP Client and how it is used with java.

Reference link:- https://en.wikipedia.org/wiki/Apache_HTTP_Server.

Written by 

KRISHNA JAISWAL is Software Consultant Trainee at Knoldus. He is passionate about JAVA , MYSQL , having knowledge of C , C++ and much more. He is recognised as a good team player, a dedicated and responsible professional, and a technology enthusiast. He is a quick learner & curious to learn new technologies. His hobbies include reading Books , listening Music and playing Cricket .