Making HTTP Calls Just Got Easier With Java 11
HTTP calls just got a lot easier, thanks to Java 11.
Join the DZone community and get the full member experience.Join For Free
Today, we will gonna discuss Http/2 client, which actually came in Java 9, but in Java 9, it was part of incubator module. In Java 11, 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
HttpURLConnection, which came in Java1.1 or which was provided by Java for Http calls prior to Java 11. After so many years, Oracle comes with a strong and beautiful API for HTTP call handling.
Http/2 is mainly divided into three classes:
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/
2Clientand as well as drawback of earlier
So, we have one mock endpoint, which we are going to use for Http requests, i.e.
Firstly, what we will do, we are going to use
HttpURLConnection, which was available for HTTP calls prior to Java 9 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.
URI uri = URI.create("http://www.mocky.io/v2/5ca5a4d1330000d22d2ea7d4");
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
To get the
Response body, we have to use 2 to 3 steps as I have done below.
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. We are getting the actual response by using
Now, we will see how to do the same thing with
HTTP/2 Client, which came in Java 11. 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
Then, we will create the
As you can see here, we are passing the request URI and request method in a single statement, and it looks good, too.
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
To get the response body and status code,
HttpResponse class provides some methods, which we can easily get the status code, status body, and response body.
I hope this blog was enjoyable and that you have gained an understanding of how to start working with
Published at DZone with permission of Kunal Sethi, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.