我正在尝试连接到需要 api 密钥的第三方 API。它使用传统的HttpURLConnection工作得很好...我收到 200 个回复
HttpURLConnection connection = (HttpURLConnection) new URL("https://www.server.com/download?apikey=<MY_KEY>"
但是,当使用Vertx WebClient(io.vertx.ext.web.client.WebClient(时,我总是得到403
禁止webClient = WebClient.create(vertx, new WebClientOptions());
webClient.get("/download")
.addQueryParam("apikey", "<MY_KEY>")
.ssl(true)
.host("www.server.com")
.port(443)
.send(downloadFileHandler ->
{
经过调查,原因似乎是因为 API 正在重定向到另一个 URL,原始 URL 和重定向都使用 SSL。不知何故,Vertx Web 客户端没有保持握手。
//WebClientOptions webClientOptions = new WebClientOptions();
WebClient client = WebClient.create(vertx, webClientOptions);
WebClientSession session = WebClientSession.create(client);
session.getAbs(url).send(response -> {
if (response.succeeded()) {
HttpResponse<Buffer> httpResponse = response.result();
System.out.println(" -> Response Code : " + httpResponse.statusCode());
promise.complete(httpResponse.bodyAsJsonObject());
} else {
promise.fail(response.cause());
}
});