当前位置: 首页>>代码示例>>Java>>正文


Java HttpClient.request方法代码示例

本文整理汇总了Java中io.vertx.core.http.HttpClient.request方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClient.request方法的具体用法?Java HttpClient.request怎么用?Java HttpClient.request使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.vertx.core.http.HttpClient的用法示例。


在下文中一共展示了HttpClient.request方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createRequest

import io.vertx.core.http.HttpClient; //导入方法依赖的package包/类
HttpClientRequest createRequest(HttpClient client, Invocation invocation, IpPort ipPort, String path,
    AsyncResponse asyncResp) {
  URIEndpointObject endpoint = (URIEndpointObject) invocation.getEndpoint().getAddress();
  RequestOptions requestOptions = new RequestOptions();
  requestOptions.setHost(ipPort.getHostOrIp())
      .setPort(ipPort.getPort())
      .setSsl(endpoint.isSslEnabled())
      .setURI(path);

  HttpMethod method = getMethod(invocation);
  LOGGER.debug("Sending request by rest, method={}, qualifiedName={}, path={}, endpoint={}.",
      method,
      invocation.getMicroserviceQualifiedName(),
      path,
      invocation.getEndpoint().getEndpoint());
  HttpClientRequest request = client.request(method, requestOptions, response -> {
    handleResponse(invocation, response, asyncResp);
  });
  return request;
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:21,代码来源:VertxHttpMethod.java

示例2: testTranslator

import io.vertx.core.http.HttpClient; //导入方法依赖的package包/类
@Test(timeout = DEFAULT_TIMEOUT)
public void testTranslator(TestContext context) throws InterruptedException {
    Async async = context.async();
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest request = client.request(HttpMethod.POST, 8080, "localhost", "/translate");
    request.putHeader("Content-type", "application/x-www-form-urlencoded");
    request.handler((response) -> {
        if (response.statusCode() == 200) {
            response.bodyHandler((buffer) -> {
                context.assertEquals("[{\"word\":\"Hello\",\"translations\":[\"ahoj\",\"dobry den\"]},{\"word\":\"world\",\"translations\":[\"svet\"]}]",
                        buffer.toString());
                client.close();
                async.complete();
            });
        }
    });
    request.end("sentence=Hello world");
}
 
开发者ID:weld,项目名称:weld-vertx,代码行数:19,代码来源:TranslatorExampleTest.java

示例3: testAdminConfigRendering

import io.vertx.core.http.HttpClient; //导入方法依赖的package包/类
@Test
public void testAdminConfigRendering() throws InterruptedException, ExecutionException, TimeoutException {

	HttpClient client = createHttpClient();
	CompletableFuture<String> future = new CompletableFuture<>();
	HttpClientRequest request = client.request(GET, "/mesh-ui/mesh-ui-config.js", rh -> {
		rh.bodyHandler(bh -> {
			if (rh.statusCode() == 200) {
				future.complete(bh.toString());
			} else {
				future.completeExceptionally(new Exception("Status code wrong {" + rh.statusCode() + "}"));
			}
		});
	});
	request.end();

	String response = future.get(10, TimeUnit.SECONDS);
	// String expectedUrl = "localhost:" + port;
	// assertTrue("The meshConfig.js file did not contain the expected url {" + expectedUrl + "} Response {" + response + "}",
	// response.contains(expectedUrl));
	// System.out.println(response);
	assertTrue("The response string should not contain any html specific characters but it was {" + response + "} ", response.indexOf("<") != 0);

}
 
开发者ID:gentics,项目名称:mesh,代码行数:25,代码来源:AdminGUIEndpointTest.java

示例4: test404Response

import io.vertx.core.http.HttpClient; //导入方法依赖的package包/类
@Test
public void test404Response() throws Exception {
	HttpClientOptions options = new HttpClientOptions();
	options.setDefaultHost("localhost");
	options.setDefaultPort(port());

	HttpClient client = Mesh.vertx().createHttpClient(options);
	CompletableFuture<String> future = new CompletableFuture<>();
	HttpClientRequest request = client.request(HttpMethod.POST, "/api/v1/test", rh -> {
		rh.bodyHandler(bh -> {
			future.complete(bh.toString());
		});
	});
	request.end();

	String response = future.get(1, TimeUnit.SECONDS);
	assertTrue("The response string should not contain any html specific characters but it was {" + response + "} ",
			response.indexOf("<") != 0);
}
 
开发者ID:gentics,项目名称:mesh,代码行数:20,代码来源:MeshRestAPITest.java

示例5: makeHttpClientRequest

import io.vertx.core.http.HttpClient; //导入方法依赖的package包/类
/**
 * Creates {@link HttpClientRequest} (Vert.x) from {@link Request} (feign).
 *
 * @param request feign request
 * @param client vertx HTTP client
 *
 * @return fully formed HttpClientRequest
 */
private HttpClientRequest makeHttpClientRequest(
    final Request request,
    final HttpClient client) throws MalformedURLException {
  final URL url = new URL(request.url());
  final int port = url.getPort() > -1
      ? url.getPort()
      : HttpClientOptions.DEFAULT_DEFAULT_PORT;
  final String host = url.getHost();
  final String requestUri = url.getFile();

  HttpClientRequest httpClientRequest = client.request(
      httpMethodFromString(request.method()),
      port,
      host,
      requestUri);

  /* Put headers to request */
  for (Map.Entry<String, Collection<String>> header :
      request.headers().entrySet()) {
    httpClientRequest = httpClientRequest.putHeader(
        header.getKey(), header.getValue());
  }

  return httpClientRequest;
}
 
开发者ID:OpenFeign,项目名称:feign-vertx,代码行数:34,代码来源:VertxHttpClient.java

示例6: doDispatch

import io.vertx.core.http.HttpClient; //导入方法依赖的package包/类
/**
 * Dispatch the request to the downstream REST layers.
 *
 * @param context routing context instance
 * @param path    relative path
 * @param client  relevant HTTP client
 */
private void doDispatch(RoutingContext context, String path, HttpClient client, Future<Object> cbFuture) {
  HttpClientRequest toReq = client
    .request(context.request().method(), path, response -> {
      response.bodyHandler(body -> {
        if (response.statusCode() >= 500) { // api endpoint server error, circuit breaker should fail
          cbFuture.fail(response.statusCode() + ": " + body.toString());
        } else {
          HttpServerResponse toRsp = context.response()
            .setStatusCode(response.statusCode());
          response.headers().forEach(header -> {
            toRsp.putHeader(header.getKey(), header.getValue());
          });
          // send response
          toRsp.end(body);
          cbFuture.complete();
        }
        ServiceDiscovery.releaseServiceObject(discovery, client);
      });
    });
  // set headers
  context.request().headers().forEach(header -> {
    toReq.putHeader(header.getKey(), header.getValue());
  });
  if (context.user() != null) {
    toReq.putHeader("user-principal", context.user().principal().encode());
  }
  // send request
  if (context.getBody() == null) {
    toReq.end();
  } else {
    toReq.end(context.getBody());
  }
}
 
开发者ID:sczyh30,项目名称:vertx-blueprint-microservice,代码行数:41,代码来源:APIGatewayVerticle.java

示例7: testRedirect

import io.vertx.core.http.HttpClient; //导入方法依赖的package包/类
@Test
public void testRedirect() throws InterruptedException, ExecutionException {
	HttpClient client = createHttpClient();
	CompletableFuture<String> future = new CompletableFuture<>();
	HttpClientRequest request = client.request(GET, "/", rh -> {
		future.complete(rh.getHeader("Location"));
	});
	request.end();
	assertEquals("/mesh-ui/", future.get());
}
 
开发者ID:gentics,项目名称:mesh,代码行数:11,代码来源:AdminGUIEndpointTest.java

示例8: proxy

import io.vertx.core.http.HttpClient; //导入方法依赖的package包/类
@Override
public Future<Buffer> proxy(RoutingContext ctx, Record record, Buffer processedBuffer) {

	// Create Proxy Future
	Future<Buffer> future = Future.future();
	final Buffer futureBuffer = Buffer.buffer();

	// Get current request
	HttpServerRequest httpSrvReq = ctx.request();
	HttpServerResponse httpSrvRes = ctx.response();
	
	String uri = extractRequestUri(httpSrvReq, record);

	// TODO get with configurations from config()
	HttpClient httpClient = discovery.getReference(record).get();
	
	// Init HttpClient request
	HttpClientRequest httpClientReq = httpClient.request(httpSrvReq.method(), uri, httpClientRes -> {

		httpSrvRes.setChunked(true);
		httpSrvRes.setStatusCode(httpClientRes.statusCode());
		httpSrvRes.headers().setAll(httpClientRes.headers());
		httpClientRes.handler(buffer -> {
			futureBuffer.appendBuffer(buffer.copy());
			httpSrvRes.write(buffer);
		});
		httpClientRes.exceptionHandler(future::fail);
		httpClientRes.endHandler((v) -> {
			httpSrvRes.end();
			future.complete(futureBuffer);
		});
	});
	
	// Handle and proxy the request
	httpClientReq.headers().setAll(httpSrvReq.headers());
	// Handle request exceptions
	httpSrvReq.exceptionHandler(future::fail);

	// Set Request chunked
	httpClientReq.setChunked(true);
	// Handle client excpetions
	httpClientReq.exceptionHandler(future::fail);
	// Call microservice
	httpClientReq.write(processedBuffer).end();

	return future;
}
 
开发者ID:pflima92,项目名称:jspare-vertx-ms-blueprint,代码行数:48,代码来源:ProxyServiceImpl.java


注:本文中的io.vertx.core.http.HttpClient.request方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。