io.vertx.ext.web.client.HttpRequest.send()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(200)

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

HttpRequest.send介绍

暂无

代码示例

代码示例来源: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);

  client.get(8080, "localhost", "/")
   .as(BodyCodec.json(User.class))
   .send(ar -> {
    if (ar.succeeded()) {
     HttpResponse<User> response = ar.result();
     System.out.println("Got HTTP response body");
     User user = response.body();
     System.out.println("FirstName " + user.firstName);
     System.out.println("LastName " + user.lastName);
     System.out.println("Male " + user.male);
    } 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: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  WebClient client = WebClient.create(vertx);

  client.get(8080, "localhost", "/")
   .as(BodyCodec.jsonObject())
   .send(ar -> {
    if (ar.succeeded()) {
     HttpResponse<JsonObject> response = ar.result();
     System.out.println("Got HTTP response body");
     System.out.println(response.body().encodePrettily());
    } else {
     ar.cause().printStackTrace();
    }
   });
 }
}

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

@Override
 public void start() throws Exception {

  WebClient client = WebClient.create(vertx);

  client.get(8080, "localhost", "/")
   .addQueryParam("firstName", "Dale")
   .addQueryParam("lastName", "Cooper")
   .addQueryParam("male", "true")
   .send(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

.putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
.putHeader("Authorization", authHeader)
.send(authHandler -> {
   .addQueryParam("q", queryToSearch)
   .putHeader("Authorization", header)
   .send(handler -> {
    if (handler.succeeded() && 200 == handler.result().statusCode()) {
     System.out.println(handler.result().body());

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

@Override
 public void start() throws Exception {

  // Create the web client and enable SSL/TLS with a trust store
  WebClient client = WebClient.create(vertx,
   new WebClientOptions()
    .setSsl(true)
    .setTrustStoreOptions(new JksOptions()
     .setPath("client-truststore.jks")
     .setPassword("wibble")
    )
  );

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

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

@Override
 public void start() throws Exception {
  vertx.eventBus().consumer("test", m -> {
   client.get(host, uri).send(ar -> {
    testContext.assertTrue(ar.succeeded());
    async.countDown();            
   });
  });
 }
};

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

@Test
public void testTimeout() throws Exception {
 AtomicInteger count = new AtomicInteger();
 server.requestHandler(req -> count.incrementAndGet());
 startServer();
 HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
 get.timeout(50).send(onFailure(err -> {
  assertTrue(err instanceof TimeoutException);
  testComplete();
 }));
 await();
}

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

@Test
public void testResponseUnknownContentTypeBodyAsJsonObject() throws Exception {
 JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
 server.requestHandler(req -> req.response().end(expected.encode()));
 startServer();
 HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
 get.send(onSuccess(resp -> {
  assertEquals(200, resp.statusCode());
  assertEquals(expected, resp.bodyAsJsonObject());
  testComplete();
 }));
 await();
}

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

@Test
public void testResponseUnknownContentTypeBodyAsJsonMapped() throws Exception {
 JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
 server.requestHandler(req -> req.response().end(expected.encode()));
 startServer();
 HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
 get.send(onSuccess(resp -> {
  assertEquals(200, resp.statusCode());
  assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.bodyAsJson(WineAndCheese.class));
  testComplete();
 }));
 await();
}

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

@Test
public void testVirtualHost() throws Exception {
 server.requestHandler(req -> {
  assertEquals("another-host:8080", req.host());
  req.response().end();
 });
 startServer();
 HttpRequest<Buffer> req = client.get("/test").virtualHost("another-host");
 req.send(onSuccess(resp -> testComplete()));
 await();
}

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

@Test
public void testMutateRequestInterceptor() throws Exception {
 server.requestHandler(req -> req.response().end());
 startServer();
 client.addInterceptor(this::handleMutateRequest);
 HttpRequest<Buffer> builder = client.get("/somepath").host("another-host").port(8081);
 builder.send(onSuccess(resp -> complete()));
 await();
}

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

@Test
public void testResponseBodyUnmarshallingError() throws Exception {
 server.requestHandler(req -> req.response().end("not-json-object"));
 startServer();
 HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
 get
  .as(BodyCodec.jsonObject())
  .send(onFailure(err -> {
  assertTrue(err instanceof DecodeException);
  testComplete();
 }));
 await();
}

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

@Test
public void testMutateResponseInterceptor() throws Exception {
 server.requestHandler(req -> req.response().setStatusCode(500).end());
 startServer();
 client.addInterceptor(this::mutateResponseHandler);
 HttpRequest<Buffer> builder = client.get("/somepath");
 builder.send(onSuccess(resp -> {
  assertEquals(200, resp.statusCode());
  complete();
 }));
 await();
}

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

@Test
public void testConnectError() throws Exception {
 HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
 get.send(onFailure(err -> {
  assertTrue(err instanceof ConnectException);
  complete();
 }));
 await();
}

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

private <R> void testResponseMissingBody(BodyCodec<R> codec) throws Exception {
 server.requestHandler(req -> req.response().setStatusCode(403).end());
 startServer();
 HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
 get
  .as(codec)
  .send(onSuccess(resp -> {
  assertEquals(403, resp.statusCode());
  assertNull(resp.body());
  testComplete();
 }));
 await();
}

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

@Test
public void testResponseBodyDiscarded() throws Exception {
 server.requestHandler(req -> req.response().end(TestUtils.randomAlphaString(1024)));
 startServer();
 HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
 get
  .as(BodyCodec.none())
  .send(onSuccess(resp -> {
  assertEquals(200, resp.statusCode());
  assertEquals(null, resp.body());
  testComplete();
 }));
 await();
}

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

@Test
public void testCacheInterceptor() throws Exception {
 server.requestHandler(req -> fail());
 startServer();
 client.addInterceptor(this::cacheInterceptorHandler);
 HttpRequest<Buffer> builder = client.get("/somepath").host("localhost").port(8080);
 builder.send(onSuccess(resp -> {
  assertEquals(200, resp.statusCode());
  complete();
 }));
 await();
}

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

@Test
public void testReadCookie(TestContext context) {
 Async async = context.async();
 prepareServer(context, req -> {
  req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test", "toast")));
 });
 client.get(PORT, "localhost", "/").send(ar -> {
  context.assertTrue(ar.succeeded());
  validate(context, client.cookieStore().get(false, "localhost", "/"),
    new String[] { "test" }, new String[] { "toast" });
  async.complete();
 });
}

相关文章