1

I am trying to make a connection to a 3rd party API that requires an api key. It works fine using traditional HttpURLConnection... I get a 200 response

HttpURLConnection connection = (HttpURLConnection) new URL("https://www.server.com/download?apikey=<MY_KEY>"

However when using the Vertx WebClient (io.vertx.ext.web.client.WebClient) I always get 403 Forbidden

webClient = WebClient.create(vertx, new WebClientOptions());
webClient.get("/download")
.addQueryParam("apikey", "<MY_KEY>")
.ssl(true)
.host("www.server.com")
.port(443)
.send(downloadFileHandler ->
{

Upon investigation it looks like the reason is because the API is redirecting to another URL, both the original URL and the redirect are using SSL. Somehow the Vertx web client is not maintaining the handshake.

Jennifer
  • 31
  • 4
  • The problem can be worked around by making two connections with two separate instances of WebClient, the first to the original URL with followRedirects set to false to allow the reading of headers to grab the redirect URL. Then making a separate request to the redirect URL to do the actual download. This is not ideal though. – Jennifer May 11 '17 at 20:57

1 Answers1

-1
//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());
    }
});
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Dec 11 '20 at 10:43