Making HTTP calls just got easier in Java11!

Reading Time: 3 minutes

Today, we will gonna discuss Http/2 client which actually came in Java9, but in Java9 it was part of incubator module. In Java11, a new module is assigned to this special functionality which is java.net.http. Now, developers don’t need to go for any third party library or API for async HTTP calls or for easy access callbacks method to get HTTP response after calls.

In this discussion, we will also try to draw a line between HTTP/2 Client and HttpURLConnection which came in Java1.1 or which was provided by Java for Http calls prior to Java11.  After so many years Oracle comes with strong or beautiful API for HTTP calls handling.

Http/2 is mainly divided into three classes:

  • HttpClient
  • HttpResponse
  • HttpRequest

So, to explain this in a more beautiful or structured way, we will try to work with one example which will actually elaborate the use case of new Http/2 Client and as well as drawback of earlier HttpURLConnection class.

So, we have one mock endpoint which we are going to use for Http requests i.e.
http://www.mocky.io/v2/5ca5a4d1330000d22d2ea7d4

Firstly,  what we will do,  we are going to use HttpURLConnection which was available for HTTP calls prior to Java9 and will illustrate its limitations as well.

Now, we have one endpoint and we just want to access that by doing HTTP call to this endpoint.

We will create the URI first by using our mock endpoint.

After this, we need to create HttpURLConnection and we explicitly need to change URI to URL. And the main thing is we need to cast the URLConnection object to HttpURLConnection explicitly which looks very ugly to me.

After that, we need to set the request method like:

To set the request method we need to pass this as a String which looks bad to me.

Now, to get the Response code there is one method named as getResponseCode()
Screenshot from 2019-04-04 15-39-03
To get the Response body, we have to use 2 to 3 steps as I have done below.

Screenshot from 2019-04-04 15-39-20
As you can see in the above screenshot, to get the response body we have to create buffered reader object and inside it, we created an input stream reader and inside it, we are getting the actual response by using connection.getInputStream().

Now, we will see how to do the same thing with HTTP/2 Client which came in Java11. And also you will get to know how easy it is and how can we overcome the challenges which we were facing earlier regarding the HTTP calls.

We are going to use the same URI which we declared earlier. As we said before the new HTTP/2  mainly depends on three classes.

Firstly, we will create the  HttpClient:Screenshot from 2019-04-04 16-29-25

Then, we will create the HttpRequest:

Screenshot from 2019-04-04 16-29-58

As you can see here we are passing the request URI and request method in a single statement and it looks good as well.

Now, we just need to send the request with the client which will give HttpResponse as an output. To add one more point here we can send the async request as well which will give the response in Future.

Screenshot from 2019-04-04 16-31-20

To get the response body and status code, HttpResponse class provides some methods using which we can easily get the status code, status body, and response body.

Screenshot from 2019-04-04 16-31-39

I hope this blog was enjoyable, and you have gained an understanding of how to start working with Http2 Client.

Knoldus-Scala-Spark-Services

Written by 

Kunal Sethi is a Software Consultant with experience of more than 1.5 years. He is a Java enthusiast and has the knowledge of various programming languages such as Scala, C++. He is familiar with Object Oriented Programming Paradigms, love to code applications in J2EE. He developed his own stand alone application in Java based on intranet chating System and email system during his Master's Degree.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!