io.vertx.ext.web.client.WebClient类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(196)

本文整理了Java中io.vertx.ext.web.client.WebClient类的一些代码示例,展示了WebClient类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebClient类的具体详情如下:
包路径:io.vertx.ext.web.client.WebClient
类名称:WebClient

WebClient介绍

暂无

代码示例

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  WebClient client = WebClient.create(vertx);

  client.get(8080, "localhost", "/").send(ar -> {
   if (ar.succeeded()) {
    HttpResponse<Buffer> response = ar.result();
    System.out.println("Got HTTP response with status " + response.statusCode() + " with data " +
     response.body().toString("ISO-8859-1"));
   } else {
    ar.cause().printStackTrace();
   }
  });
 }
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  WebClient client = WebClient.create(vertx);

  MultiMap form = MultiMap.caseInsensitiveMultiMap();
  form.add("firstName", "Dale");
  form.add("lastName", "Cooper");
  form.add("male", "true");

  client.post(8080, "localhost", "/").sendForm(form, ar -> {
   if (ar.succeeded()) {
    HttpResponse<Buffer> response = ar.result();
    System.out.println("Got HTTP response with status " + response.statusCode());
   } else {
    ar.cause().printStackTrace();
   }
  });
 }
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  WebClient client = WebClient.create(vertx);

  Buffer body = Buffer.buffer("Hello World");

  client.put(8080, "localhost", "/").sendBuffer(body, ar -> {
   if (ar.succeeded()) {
    HttpResponse<Buffer> response = ar.result();
    System.out.println("Got HTTP response with status " + response.statusCode());
   } else {
    ar.cause().printStackTrace();
   }
  });
 }
}

代码示例来源:origin: vert-x3/vertx-examples

private void invoke(RoutingContext rc) {
  client.get("/luke").as(BodyCodec.string()).send(ar -> {
   if (ar.failed()) {
    rc.response().end("Unable to call the greeting service: " + ar.cause().getMessage());
   } else {
    if (ar.result().statusCode() != 200) {
     rc.response().end("Unable to call the greeting service - received status:" + ar.result().statusMessage());
    } else {
     rc.response().end("Greeting service invoked: '" + ar.result().body() + "'");
    }
   }
  });
 }
}

代码示例来源:origin: io.vertx/vertx-web-client

@Test
public void testCustomUserAgent() throws Exception {
 client = WebClient.wrap(super.client, new WebClientOptions().setUserAgent("smith"));
 testRequest(client -> client.get("somehost", "somepath"), req -> assertEquals(Collections.singletonList("smith"), req.headers().getAll(HttpHeaders.USER_AGENT)));
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Create a web client using the provided <code>vertx</code> instance and default options.
 * @param vertx the vertx instance
 * @return the created web client
 */
public static io.vertx.rxjava.ext.web.client.WebClient create(io.vertx.rxjava.core.Vertx vertx) { 
 io.vertx.rxjava.ext.web.client.WebClient ret = io.vertx.rxjava.ext.web.client.WebClient.newInstance(io.vertx.ext.web.client.WebClient.create(vertx.getDelegate()));
 return ret;
}

代码示例来源:origin: vert-x3/vertx-examples

WebClient client = WebClient.create(vertx);
client.postAbs(AUTH_URL)
 .as(BodyCodec.jsonObject())
 .addQueryParam("grant_type", "client_credentials")
   String header = "Bearer " + accessToken;
   client.getAbs(TWEET_SEARCH_URL)
    .as(BodyCodec.jsonObject())
    .addQueryParam("q", queryToSearch)

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Close the client. Closing will close down any pooled connections.
 * Clients should always be closed after use.
 */
public void close() { 
 delegate.close();
}

代码示例来源:origin: io.vertx/vertx-web-client

private void testTLS(WebClientOptions clientOptions, HttpServerOptions serverOptions, Function<WebClient, HttpRequest<Buffer>> requestProvider, Consumer<HttpServerRequest> serverAssertions) throws Exception {
 WebClient sslClient = WebClient.create(vertx, clientOptions);
 HttpServer sslServer = vertx.createHttpServer(serverOptions);
 sslServer.requestHandler(req -> {
  assertEquals(serverOptions.isSsl(), req.isSSL());
  if (serverAssertions != null) {
   serverAssertions.accept(req);
  }
  req.response().end();
 });
 try {
  startServer(sslServer);
  HttpRequest<Buffer> builder = requestProvider.apply(sslClient);
  builder.send(onSuccess(resp -> testComplete()));
  await();
 } finally {
  sslClient.close();
  sslServer.close();
 }
}

代码示例来源:origin: io.vertx/vertx-web-client

private void testRequest(HttpMethod method) throws Exception {
 testRequest(client -> {
  switch (method) {
   case GET:
    return client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
   case HEAD:
    return client.head(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
   case DELETE:
    return client.delete(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
   default:
    fail("Invalid HTTP method");
    return null;
  }
 }, req -> assertEquals(method, req.method()));
}

代码示例来源:origin: io.vertx/vertx-web-client

@Test
public void testStreamHttpServerRequest() throws Exception {
 Buffer expected = TestUtils.randomBuffer(10000);
 HttpServer server2 = vertx.createHttpServer(new HttpServerOptions().setPort(8081)).requestHandler(req -> req.bodyHandler(body -> {
  assertEquals(body, expected);
  req.response().end();
 }));
 startServer(server2);
 WebClient webClient = WebClient.create(vertx);
 try {
  server.requestHandler(req -> webClient.postAbs("http://localhost:8081/")
   .sendStream(req, onSuccess(resp -> req.response().end("ok"))));
  startServer();
  webClient.post(8080, "localhost", "/").sendBuffer(expected, onSuccess(resp -> {
   assertEquals("ok", resp.bodyAsString());
   complete();
  }));
  await();
 } finally {
  server2.close();
 }
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Wrap an <code>httpClient</code> with a web client and default options.
 * @param httpClient the  to wrap
 * @return the web client
 */
public static io.vertx.rxjava.ext.web.client.WebClient wrap(io.vertx.rxjava.core.http.HttpClient httpClient) { 
 io.vertx.rxjava.ext.web.client.WebClient ret = io.vertx.rxjava.ext.web.client.WebClient.newInstance(io.vertx.ext.web.client.WebClient.wrap(httpClient.getDelegate()));
 return ret;
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Create an HTTP POST request to send to the server at the specified host and default port.
 * @param host the host
 * @param requestURI the relative URI
 * @return an HTTP client request object
 */
public io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> post(String host, String requestURI) { 
 io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.post(host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG);
 return ret;
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Create an HTTP request to send to the server at the specified host and port.
 * @param method the HTTP method
 * @param options the request options
 * @return an HTTP client request object
 */
public io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> request(HttpMethod method, RequestOptions options) { 
 io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.request(method, options), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG);
 return ret;
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Create an HTTP DELETE request to send to the server at the specified host and port.
 * @param port the port
 * @param host the host
 * @param requestURI the relative URI
 * @return an HTTP client request object
 */
public io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> delete(int port, String host, String requestURI) { 
 io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.delete(port, host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG);
 return ret;
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Create an HTTP GET request to send to the server at the specified host and default port.
 * @param host the host
 * @param requestURI the relative URI
 * @return an HTTP client request object
 */
public io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> get(String host, String requestURI) { 
 io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.get(host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG);
 return ret;
}

代码示例来源:origin: io.vertx/vertx-web-client

@Test
public void testUserAgentDisabled() throws Exception {
 client = WebClient.wrap(super.client, new WebClientOptions().setUserAgentEnabled(false));
 testRequest(client -> client.get("somehost", "somepath"), req -> assertEquals(Collections.emptyList(), req.headers().getAll(HttpHeaders.USER_AGENT)));
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Create a web client using the provided <code>vertx</code> instance.
 * @param vertx the vertx instance
 * @param options the Web Client options
 * @return the created web client
 */
public static io.vertx.rxjava.ext.web.client.WebClient create(io.vertx.rxjava.core.Vertx vertx, WebClientOptions options) { 
 io.vertx.rxjava.ext.web.client.WebClient ret = io.vertx.rxjava.ext.web.client.WebClient.newInstance(io.vertx.ext.web.client.WebClient.create(vertx.getDelegate(), options));
 return ret;
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  WebClient client = WebClient.create(vertx);

  JsonObject user = new JsonObject()
   .put("firstName", "Dale")
   .put("lastName", "Cooper")
   .put("male", true);

  client.put(8080, "localhost", "/").sendJson(user, ar -> {
   if (ar.succeeded()) {
    HttpResponse<Buffer> response = ar.result();
    System.out.println("Got HTTP response with status " + response.statusCode());
   } else {
    ar.cause().printStackTrace();
   }
  });
 }
}

代码示例来源:origin: vert-x3/vertx-config

/**
 * Closes the client.
 */
public void close() {
 if (client != null) {
  client.close();
 }
}

相关文章