當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。