Java 9 is feature complete and one of the features is the new HTTP Client library, that will likely replace the existing HttpUrlConnection library. In this post you can read a few tests I made using this library.
To support HTTP 1 and 2, the new API brings 4 new main classes as you can see java.net.http package summary:
In this post we will focus ton HttpClient, HttpRequest and HttpResponse. Notice that the code I will show is slightly different from the one you see in the online javadocs - the API is still in development and it is a subject to change!
JShell is awesome, you can run jshell binary that comes with Java 9 and start writing Java code. I recommend this article if you want to know more about it.
However, there's a small problem if you want to use the new API you must add the module java.httpclient. Yes, Java 9 has a modular system and not all modules are imported by default. See in HttpClient javadoc. In top left side you can see the module information. Some modules are not imported by default and you must start jshell with the --add-modules and I have an error when adding java.httpclient. I know it is a specific error for java.httpclient because it works with other modules, like java.logging. I also tried to specify the module path using --module-path pointing to the jmods directory (because I see java.httpclient module there), but it was unsuccessful.
I found that the reason is the fact that the HttpClient is on JDK incubator module jdk jdk.incubator.httpclient. The version I am using is build 9-ea+153.
[wsiqueir@wsiqueir jdk-9]$ ./bin/jshell --add-modules jdk.incubator.httpclient
The new HTTP API
To support HTTP 1 and 2, the new API brings 4 new main classes as you can see java.net.http package summary:
In this post we will focus ton HttpClient, HttpRequest and HttpResponse. Notice that the code I will show is slightly different from the one you see in the online javadocs - the API is still in development and it is a subject to change!
Using HttpRequest, HttpResponse and HttpClient on jshell
JShell is awesome, you can run jshell binary that comes with Java 9 and start writing Java code. I recommend this article if you want to know more about it.
However, there's a small problem if you want to use the new API you must add the module java.httpclient. Yes, Java 9 has a modular system and not all modules are imported by default. See in HttpClient javadoc. In top left side you can see the module information. Some modules are not imported by default and you must start jshell with the --add-modules and I have an error when adding java.httpclient. I know it is a specific error for java.httpclient because it works with other modules, like java.logging. I also tried to specify the module path using --module-path pointing to the jmods directory (because I see java.httpclient module there), but it was unsuccessful.
I found that the reason is the fact that the HttpClient is on JDK incubator module jdk jdk.incubator.httpclient. The version I am using is build 9-ea+153.
[wsiqueir@wsiqueir jdk-9]$ ./bin/jshell --add-modules jdk.incubator.httpclient
Now I am ready to test the API using jshell! Just one thing to notice: The online HTTPClient documentation is not the same as in the version I showed above, we should trust the documentation that comes with the JDK bits!
Simple HTTP request
You need 3 lines of code to make an HTTP request. You could transform this in one liner code:
String body = HttpClient.newHttpClient().send(HttpRequest.newBuilder(URI.create("http://www.google.com")).GET().build(), HttpResponse.BodyHandler.asString()).body()
mmmmm, better not.
String body = HttpClient.newHttpClient().send(HttpRequest.newBuilder(URI.create("http://www.google.com")).GET().build(), HttpResponse.BodyHandler.asString()).body()
mmmmm, better not.
Using Basic Authentication
I didn't like having to write a subclass of Authenticator just to set my credentials - that must be better way to achieve this, but at least for now we know it works greatly!
Setting HTTP Headers
As you can see you can set HTTP headers when creating a new request.
Conclusion
We tested the new Http Client API! I will stop here, but you can continue and test:
* Proxy configuration
* Use an executor
* SSL parameters
I saw all these methods on HttpClient and HttpBuilder methods - it is a full and modern Http client, guys! The only thing I didn't like is how we configure BASIC connection, but, as I mentioned, this code is a subject to change. Here's the code if you want to copy and paste it:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Basic request | |
import jdk.incubator.http.*; | |
HttpClient client = HttpClient.newHttpClient(); | |
HttpRequest request = HttpRequest.newBuilder(URI.create("http://www.google.com")).GET().build(); | |
HttpResponse response = client.send(request, HttpResponse.BodyHandler.asString()) | |
System.out.println(response.body()) | |
-- | |
// setting authentication | |
import jdk.incubator.http.* | |
public class MyAuthenticator extends Authenticator { | |
String usr = "kieserver"; | |
char[] psw = "kieserver1!".toCharArray(); | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication(usr, psw); | |
} | |
} | |
URI uri = URI.create("http://localhost:8180/kie-server/services/rest/server") | |
HttpClient client = HttpClient.newBuilder().authenticator(new MyAuthenticator()).build(); | |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build() | |
HttpResponse response = client.send(request, HttpResponse.BodyHandler.asString()) | |
System.out.println(response.body()) | |
-- | |
// setting http headers | |
HttpRequest request = HttpRequest.newBuilder(uri).header("Accept", "application/json").GET().build() | |
-- | |
//one liner | |
String body = HttpClient.newHttpClient().send( | |
HttpRequest.newBuilder(URI.create("http://www.google.com")).GET().build(), | |
HttpResponse.BodyHandler.asString() | |
).body(); |
Comentários
Postar um comentário