I am using vert.x with JAVA on the server side. when client goes to http://localhost:8080/hello, I want the browser to go to "google.com". I get an error when executing the GET request

    router.route("/hello").handler(routingContext -> {
        String url = "google.com";
        WebClient client = WebClient.create(vertx, new WebClientOptions().setSsl(true).setTrustAll(true).setDefaultPort(8080).setKeepAlive(true).setDefaultHost(url));
        client.get(url).as(BodyCodec.string()).send(ar -> {
            if(ar.succeeded()) {
                HttpResponse<String> response = ar.result();
                System.out.println("Got HTTP response body");
                System.out.println(response.body().toString());                 
            }
            else {
                ar.cause().printStackTrace();
            }
        });

    });

Error:

io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection timed out: no further information: google.com/172.217.16.142:8080

1 Answers

0
Alexey Soshin On Best Solutions

Let's sort out some things.

First, cross origin is relevant to browsers. You're issuing a server-to-server request, so it's not relevant here.

Second, I hope you don't actually need to issue request to google.com, as Google actually tries to stop others to use its search page that way.

Third, you're using your url argument twice. Once when you set your default host, and second time when issuing a get() request. Then you also set port to 8080, which google.com doesn't expose last time I checked.

Which produces something like:

https://google.com:8080/google.com

To receive response that makes a little bit more sense, you can try this code (I removed the routing part):

Vertx vertx = Vertx.vertx();

    String url = "api.openweathermap.org";
    WebClient client = WebClient.create(vertx, new WebClientOptions().setDefaultPort(80).setDefaultHost(url));
    client.get("/data/2.5/weather?q=london,uk&units=metric&appid=e38f373567e83d2ba1b6928384435689").as(BodyCodec.string()).send(ar -> {
        if(ar.succeeded()) {
            HttpResponse<String> response = ar.result();
            System.out.println("Got HTTP response body");
            System.out.println(response.body());
        }
        else {
            ar.cause().printStackTrace();
        }
    });