1、加入依赖
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
<version>3.8.5</version>
</dependency>
2、编写GET请求
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
public class MainVerticle extends AbstractVerticle {
public static void main(String[] args) {
Vertx.vertx().deployVerticle(new MainVerticle());
}
@Override
public void start() throws Exception {
WebClient client = WebClient.create(vertx);
client.getAbs("http://www.baidu.com").send(ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
System.out.println(response.body().toString());
} else {
System.out.println("发送请求失败:" + ar.cause().getMessage());
}
});
}
}
POST请求和更多解释请到我的独立博客看原文 https://dev-tang.com/post/2020/06/httpclient-vert.x.html