Search in sources :

Example 16 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions()).put(8080, "localhost", "/someurl", resp -> {
        System.out.println("Response " + resp.statusCode());
    });
    String filename = "upload.txt";
    FileSystem fs = vertx.fileSystem();
    fs.props(filename, ares -> {
        FileProps props = ares.result();
        System.out.println("props is " + props);
        long size = props.size();
        req.headers().set("content-length", "" + size);
        fs.open(filename, new OpenOptions(), ares2 -> {
            AsyncFile file = ares2.result();
            Pump pump = Pump.pump(file, req);
            file.endHandler(v -> {
                req.end();
            });
            pump.start();
        });
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) FileSystem(io.vertx.core.file.FileSystem) AsyncFile(io.vertx.core.file.AsyncFile) FileProps(io.vertx.core.file.FileProps) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Pump(io.vertx.core.streams.Pump)

Example 17 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClientOptions options = new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2);
    vertx.createHttpClient(options).getNow(8080, "localhost", "/", resp -> {
        System.out.println("Got response " + resp.statusCode() + " with protocol " + resp.version());
        resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1")));
    });
}
Also used : HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 18 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    // Note! in real-life you wouldn't often set trust all to true as it could leave you open to man in the middle attacks.
    HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setTrustAll(true);
    HttpClient client = vertx.createHttpClient(options);
    HttpClientRequest request = client.get(8443, "localhost", "/", resp -> {
        System.out.println("Got response " + resp.statusCode() + " with protocol " + resp.version());
        resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1")));
    });
    // Set handler for server side push
    request.pushHandler(pushedReq -> {
        System.out.println("Receiving pushed content");
        pushedReq.handler(pushedResp -> {
            pushedResp.bodyHandler(body -> System.out.println("Got pushed data " + body.toString("ISO-8859-1")));
        });
    });
    request.end();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 19 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClientRequest request = vertx.createHttpClient(new HttpClientOptions()).put(8080, "localhost", "/", resp -> {
        System.out.println("Got response " + resp.statusCode());
        resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1")));
    });
    request.setChunked(true);
    for (int i = 0; i < 10; i++) {
        request.write("client-chunk-" + i);
    }
    request.end();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 20 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-examples by vert-x3.

the class Proxy method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient(new HttpClientOptions());
    vertx.createHttpServer().requestHandler(req -> {
        System.out.println("Proxying request: " + req.uri());
        HttpClientRequest c_req = client.request(req.method(), 8282, "localhost", req.uri(), c_res -> {
            System.out.println("Proxying response: " + c_res.statusCode());
            req.response().setChunked(true);
            req.response().setStatusCode(c_res.statusCode());
            req.response().headers().setAll(c_res.headers());
            c_res.handler(data -> {
                System.out.println("Proxying response body: " + data.toString("ISO-8859-1"));
                req.response().write(data);
            });
            c_res.endHandler((v) -> req.response().end());
        });
        c_req.setChunked(true);
        c_req.headers().setAll(req.headers());
        req.handler(data -> {
            System.out.println("Proxying request body " + data.toString("ISO-8859-1"));
            c_req.write(data);
        });
        req.endHandler((v) -> c_req.end());
    }).listen(8080);
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Runner(io.vertx.example.util.Runner) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Aggregations

HttpClientOptions (io.vertx.core.http.HttpClientOptions)101 Test (org.junit.Test)51 HttpClient (io.vertx.core.http.HttpClient)27 HttpClientRequest (io.vertx.core.http.HttpClientRequest)23 HttpServerOptions (io.vertx.core.http.HttpServerOptions)19 CountDownLatch (java.util.concurrent.CountDownLatch)15 HttpMethod (io.vertx.core.http.HttpMethod)14 SSLCustom (org.apache.servicecomb.foundation.ssl.SSLCustom)14 SSLOption (org.apache.servicecomb.foundation.ssl.SSLOption)14 HttpVersion (io.vertx.core.http.HttpVersion)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Vertx (io.vertx.core.Vertx)11 MockUp (mockit.MockUp)11 DeploymentOptions (io.vertx.core.DeploymentOptions)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 Buffer (io.vertx.core.buffer.Buffer)9 HttpServer (io.vertx.core.http.HttpServer)8 HttpServerResponse (io.vertx.core.http.HttpServerResponse)8 VertxOptions (io.vertx.core.VertxOptions)7 VertxInternal (io.vertx.core.impl.VertxInternal)7