本文整理汇总了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;
}
示例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");
}
示例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);
}
示例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);
}
示例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;
}
示例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());
}
}
示例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());
}
示例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;
}