Since Java 1.1 there has been an HTTP client in the core libraries provided with the JDK. With Java 11 a new client was added. One of these might be a good choice if you are sensitive about adding extra dependencies to your project.
Java 1.1 HttpURLConnection
First of all, do we capitalize acronyms in class names or not? Make your mind up. Anyway, close your eyes and center yourself in 1997. Titanic was rocking the box office and inspiring a thousand memes, Spice Girls had a best-selling album, but the biggest news of the year was surely HttpURLConnection being added to Java 1.1. Here’s how you would use it to make a GET request to get
the APOD data:
APOD class:
1 | public class APOD { |
JSON data:
1 | { |
1 | public class JavaHttpURLConnectionDemo { |
This seems quite verbose, and I find the order that we have to do things is confusing (why do we set headers after opening the URL?). If you need to make more complex requests with POST
bodies, or custom timeouts etc then it’s all possible but I never found this API intuitive at all.
When would you use HTTPUrlConnection
, then? If you are supporting clients who are using older versions of Java, and you can’t add a dependency then this might be for you. I suspect that’s only a small minority of developers, but you might see it in older codebases - for more modern approaches, read on.
Java 11 HttpClient
More than twenty years after HttpURLConnection
we had Black Panther in the cinemas and a new HTTP client added to Java 11: java.net.http.HttpClient. This has a much more logical API and can handle HTTP/2, and Websockets. It also has the option to make requests synchronously or asynchronously by using the CompletableFuture
API.
99 times out of 100 when I make an HTTP request I want to read the response body into my code. Libraries that make this difficult will not spark joy in me. HttpClient accepts a BodyHandler
which can convert an HTTP response into a class of your choosing. There are some built-in handlers: String
, byte[]
for binary data, Stream<String>
which splits by lines, and a few others. You can also define your own, which might be helpful as there isn’t a built-in BodyHandler
for parsing JSON. I’ve written one (here) based on Jackson following an example from Java Docs. It returns a Supplier
for the APOD class, so we call .get()
when we need the result.
This is a synchronous request:
1 | // create a client |
For an asynchronous request the client
and request
are made in the same way, then call .sendAsync
instead of .send
:
1 | // use the client to send the request |
full code on GitHub:https://github.com/mjg123/java-http-clients
Reference Resources:https://www.twilio.com/blog/5-ways-to-make-http-requests-in-java
Another Demo
1 | /** |
Reference Resources:https://www.cnblogs.com/swordfall/p/10757499.html#auto_id_5
Case Source Code:https://github.com/V-Vincen/calling-third-party-api
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !