“This is the 20th day of my participation in the August Gwen Challenge
VertX Web Client dependency
MAVEN
< the dependency > < groupId > IO. Vertx < / groupId > < artifactId > vertx - web - client < / artifactId > < version > 4.1.2 < / version > </dependency>Copy the code
Gradle
Dependencies {compile 'IO. Vertx :vertx-web-client:4.1.2'}Copy the code
Create a Web client
WebClient client = WebClient.create(vertx);
Copy the code
If you want to configure options for customers, create it as follows
WebClientOptions options = new WebClientOptions().setUserAgent(" my-app /1.2.3"); options.setKeepAlive(false); WebClient client = WebClient.create(vertx, options);Copy the code
The Web client option inherits the Http client option and can still be used if an Http client is already in the application
WebClient client = WebClient.create(vertx); fs.open("content.txt", new OpenOptions(), fileRes -> { if (fileRes.succeeded()) { ReadStream<Buffer> fileStream = fileRes.result(); String fileLen = "1024"; // Send the file to the server using POST client .post(8080, "myserver.mycompany.com", "/some-uri") .putHeader("content-length", fileLen) .sendStream(fileStream) .onSuccess(res -> { // OK }) ; }});Copy the code
A post request
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendBuffer(buffer)
.onSuccess(res -> {
// OK
});
Copy the code
Sending buffers directly is supported, but if you don’t want to load content directly into memory, or if you want to handle many concurrent requests and want each request to be a minimum. To do this, Web clients can send using stream (“ReadStream”)
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendStream(stream)
.onSuccess(res -> {
// OK
});
Copy the code
Json
When sending JSON, use sendJsonObject
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendJsonObject(
new JsonObject()
.put("firstName", "Dale")
.put("lastName", "Cooper"))
.onSuccess(res -> {
// OK
});
Copy the code
The Web client is responsible for setting up the Transfer pump for you. It is not known that the request will use a large Transfer encoding due to the length of the stream.
When the size of the stream is known, you should specify Content-Length before using the header
fs.open("content.txt", new OpenOptions(), fileRes -> { if (fileRes.succeeded()) { ReadStream<Buffer> fileStream = fileRes.result(); String fileLen = "1024"; // Send the file to the server using POST client .post(8080, "myserver.mycompany.com", "/some-uri") .putHeader("content-length", fileLen) .sendStream(fileStream) .onSuccess(res -> { // OK }) ; }});Copy the code
form
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.set("firstName", "Dale");
form.set("lastName", "Cooper");
// Submit the form as a form URL encoded body
client
.post(8080, "myserver.mycompany.com", "/some-uri")
.sendForm(form)
.onSuccess(res -> {
// OK
});
Copy the code
Reuse request
This method can be safely called multiple times, so it is easy to configure and reuse the object Send ‘ ‘HttpRequest
HttpRequest<Buffer> get = client
.get(8080, "myserver.mycompany.com", "/some-uri");
get
.send()
.onSuccess(res -> {
// OK
});
// Same request again
get
.send()
.onSuccess(res -> {
// OK
});
Copy the code
Be careful, although instances are mutable. Therefore, you should call this method before modifying the cache instance. HttpRequest“copy
HttpRequest<Buffer> get = client
.get(8080, "myserver.mycompany.com", "/some-uri");
get
.send()
.onSuccess(res -> {
// OK
});
// The "get" request instance remains unmodified
get
.copy()
.putHeader("a-header", "with-some-value")
.send()
.onSuccess(res -> {
// OK
});
Copy the code
timeout
You can set timeouts with specific HTTP requests. timeout
client .get(8080, "myserver.mycompany.com", "/some-uri") .timeout(5000) .send() .onSuccess(res -> { // OK }) .onFailure(err -> { // Might be a timeout when cause is java.util.concurrent.TimeoutException });Copy the code
If the request does not return any data during the timeout, the exception is passed to the response handler.
Handling HTTP responses
When a Web client sends a request, a single same-item result is always processed. HttpResponse
In a successful result, the callback occurs after the response is received
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.send()
.onSuccess(res ->
System.out.println("Received response with status code" + res.statusCode()))
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
Copy the code
By default, vert. x Web client requests only end in an error if an error occurs at the network level. In other words, a response or a response of the wrong content type is not considered a failure. If you want the Web client to automatically perform sanity checks, use Response predicates. 404 Not Found