Hypertext Transfer Protocol (HTTP) is an application protocol that is, basically used as a request/response mechanism for data transformation. HTTP is based on the Client/Server model. Client/Server model can be explained as two computers, Client (receiver of service) and Server (provider of service) that are communicating via requests and responses.
Talking about earlier HTTP which is currently, the foundation of data communication for the World Wide Web. WWW came back in 1990s. Since then, the world has changed a lot and so is the man. He has become impatient. He requires instant results and can’t wait to get the response back. Even the recent researches, they claim that in order to wait for one second, there was a loss of 7% revenue which cannot be afforded. So, after almost 16 years of struggle, they finally came with Http/2 Client which solves a lot of problems.
This new API is build around three main classes :
We will learn how exactly this solved the problem with the help of an example.
We will be using a default URI for the demo purpose that is, http://www.example.com which contains the data as shown in the image below.
Let’s create a URI with the default one provided i.e http://www.example.com which will be used in both HTTP and Http/2. The code snippets are attached after the last step for both the clients.
URI uri = new URI("http://www.example.com");
How it was done with HTTP ?
1. Open up the connection. In order to do so, firstly, we have to convert URI to URL and then cast it explicitly to HttpURLConnection.
HttpURLConnection httpURLConnection =(HttpURLConnection)uri.toURL().openConnection()
2. Specifying whether it is a GET or POST call, we have to pass the value as String in the method setRequestMethod, another boiler-plating of code.
3. Now, let’s call our method to check if it is working.
4. We can see the response code as 200, but we will need the data received by requesting the above URL. How to do that? Firstly, we need to call the method getInputStream(), cast it into InputStreamReader, then again cast this into BufferedReader and call the method readLine().
new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())).readLine()
This still this does not provide me the whole data but just the first line. So, we need to add this in a loop and then get the complete data.
All these boiler-plating of code is now reduced with Http2 Client, thus making it faster and more efficient in terms of time as well as performance.
How it is done with Http/2 ?
1. Since the Http2 is build around the three classes as explained above, the first step will be to import them.
2. Now, we will create our http client.
HttpClient httpClient = HttpClient.newHttpClient()
3. Create HttpRequest, which has a method called uri(). It also has methods to specify the type of request that is, GET() or POST() or PATCH() or DELETE().
HttpRequest httpRequest = HttpRequest.newBuilder().uri(uri).GET().build()
4. Now, we have our client and request. All we need to do is send the request. (As of now, I have made synchronous call, just for demo purpose, but there do exist method to call it asynchronously as well). The send method requires the request and a request body handler.
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString())
Now, we get the status code or the response body.
This provides me the whole data and making the code more readable with less amount of boiler-plating. Thus, it improves the performance and reduces the time to get the response.
Want to know all the new main features of HTTP/2 like how it dramatically improve latency on the web and how to write HTTP 2.0 applications in the Java platform? Then, register for our upcoming webinar – “Draw a line between Http/1.1 and Http/2 Client” with Kunal Sethi, Software Consultant at Knoldus Inc. on 18th July, 2019 at 10:00 PM IST / 12:30 PM EDT to learn all about it with a live demo session.
Note: in Java9 it was part of the incubator module. In Java11, a new module is assigned to this special functionality which is java.net.http.