本文整理汇总了Java中io.vertx.rxjava.core.http.HttpClient类的典型用法代码示例。如果您正苦于以下问题:Java HttpClient类的具体用法?Java HttpClient怎么用?Java HttpClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpClient类属于io.vertx.rxjava.core.http包,在下文中一共展示了HttpClient类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: doDispatch
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
private void doDispatch(RoutingContext ctx, String path, ServiceReference ref) {
HttpClientRequest req = ref.getAs(HttpClient.class).request(ctx.request().method(), path,
res -> res.bodyHandler(body -> check(res.statusCode() >= 500,
() -> serviceUnavailable(ctx, res.statusMessage()),
() -> chain(ctx.response(), r -> res.headers().addAll(r.headers()))
.setStatusCode(res.statusCode())
.end(body),
() -> discovery.release(ref))));
req.headers().addAll(ctx.request().headers());
// TODO: 24.08.2017 pass user auth data to service?, (in header)
if (ctx.getBody() != null) {
req.end(ctx.getBody());
return;
}
req.end();
}
开发者ID:kristenkotkas,项目名称:moviediary,代码行数:17,代码来源:GatewayVerticle.java
示例2: testHello
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
@Test
public void testHello(TestContext test) {
HttpClient client = Vertx.vertx().createHttpClient();
HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/hello");
req.toObservable()
.flatMap(res -> {
if (res.statusCode() != 200) {
test.fail("status.code -> " + res.statusCode());
}
return res.toObservable();
})
.subscribe(data -> {
logger.info("Server content " + data.toString("UTF-8"));
});
req.end();
}
开发者ID:takecy,项目名称:vertx3-api-server,代码行数:19,代码来源:RxApiServerTest.java
示例3: testIndex
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
@Test
public void testIndex(TestContext test) {
HttpClient client = Vertx.vertx().createHttpClient();
HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
req.toObservable()
.flatMap(res -> {
test.assertEquals(res.statusCode(), 303);
test.assertEquals(res.headers().get(HttpHeaders.LOCATION), is("hello"));
return res.toObservable();
})
.subscribe(data -> {
logger.info("Server content " + data.toString("UTF-8"));
});
req.end();
}
开发者ID:takecy,项目名称:vertx3-api-server,代码行数:19,代码来源:RxApiServerTest.java
示例4: testHttpClient
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
@Test
public void testHttpClient() {
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
server.requestStream().handler(req -> {
req.response().setChunked(true).end("some_content");
});
try {
server.listen(ar -> {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
client.request(HttpMethod.GET, 8080, "localhost", "/the_uri", resp -> {
Buffer content = Buffer.buffer();
Observable<Buffer> observable = resp.toObservable();
observable.forEach(content::appendBuffer, err -> fail(), () -> {
assertEquals("some_content", content.toString("UTF-8"));
testComplete();
});
}).end();
});
await();
} finally {
server.close();
}
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:24,代码来源:CoreApiTest.java
示例5: testWebsocketClient
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
@Test
public void testWebsocketClient() {
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
server.websocketStream().handler(ws -> {
ws.write(Buffer.buffer("some_content"));
ws.close();
});
server.listen(ar -> {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
client.websocket(8080, "localhost", "/the_uri", ws -> {
Buffer content = Buffer.buffer();
Observable<Buffer> observable = ws.toObservable();
observable.forEach(content::appendBuffer, err -> fail(), () -> {
server.close();
assertEquals("some_content", content.toString("UTF-8"));
testComplete();
});
});
});
await();
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:22,代码来源:CoreApiTest.java
示例6: testWebsocketClientFlatMap
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
@Test
public void testWebsocketClientFlatMap() {
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
server.websocketStream().handler(ws -> {
ws.write(Buffer.buffer("some_content"));
ws.close();
});
server.listen(ar -> {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
Buffer content = Buffer.buffer();
client.
websocketStream(8080, "localhost", "/the_uri").
toObservable().
flatMap(WebSocket::toObservable).
forEach(content::appendBuffer, err -> fail(), () -> {
server.close();
assertEquals("some_content", content.toString("UTF-8"));
testComplete();
});
});
await();
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:23,代码来源:CoreApiTest.java
示例7: RecentPurchasesHandler
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
public RecentPurchasesHandler(HttpClient client, SharedData sharedData) {
this.client = client;
usersCache = sharedData.getLocalMap("users");
productsCache = sharedData.getLocalMap("products");
userPurchasesCache = sharedData.getLocalMap("user-purchases");
productPurchasesCache = sharedData.getLocalMap("product-purchases");
}
开发者ID:MiguelGL,项目名称:popular-purchases-demo,代码行数:9,代码来源:RecentPurchasesHandler.java
示例8: websocketClient
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
public void websocketClient(Vertx vertx) {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
client.websocketStream(8080, "localhost", "/the_uri").toObservable().subscribe(
ws -> {
// Use the websocket
},
error -> {
// Could not connect
}
);
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:12,代码来源:RxifiedExamples.java
示例9: httpClientRequest
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
public void httpClientRequest(Vertx vertx) {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
HttpClientRequest request = client.request(HttpMethod.GET, 8080, "localhost", "/the_uri");
request.toObservable().subscribe(
response -> {
// Process the response
},
error -> {
// Could not connect
}
);
request.end();
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:14,代码来源:RxifiedExamples.java
示例10: get
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
/**
* @see #get(HttpClient, int, String, String, MultiMap)
*/
public static Observable<HttpClientResponse> get(HttpClient client, String requestURI) {
return Observable.create(subscriber -> {
HttpClientRequest req = client.get(requestURI);
Observable<HttpClientResponse> resp = req.toObservable();
resp.subscribe(subscriber);
req.end();
});
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:12,代码来源:RxHelper.java
示例11: testHttpClientConnectionFailure
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
@Test
public void testHttpClientConnectionFailure() {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
HttpClientRequest req = client.request(HttpMethod.GET, 9998, "255.255.255.255", "/the_uri");
Observable<HttpClientResponse> obs = req.toObservable();
obs.subscribe(
buffer -> fail(),
err -> testComplete(),
this::fail);
req.end();
await();
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:13,代码来源:CoreApiTest.java
示例12: testHttpClientConnectionFailureFlatMap
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
@Test
public void testHttpClientConnectionFailureFlatMap() {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
HttpClientRequest req = client.request(HttpMethod.GET, 9998, "255.255.255.255", "/the_uri");
Observable<HttpClientResponse> obs = req.toObservable();
obs.flatMap(HttpClientResponse::toObservable).forEach(
buffer -> fail(),
err -> testComplete(),
this::fail);
req.end();
await();
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:13,代码来源:CoreApiTest.java
示例13: testGetHelper
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
@Test
public void testGetHelper() throws Exception {
CountDownLatch listenLatch = new CountDownLatch(1);
HttpServer server = vertx.createHttpServer();
AtomicInteger count = new AtomicInteger();
server.requestHandler(req -> {
req.response().end(Buffer.buffer("request=" + count.getAndIncrement()));
}).listen(8080, onSuccess(s -> {
listenLatch.countDown();
}));
awaitLatch(listenLatch);
HttpClient client = vertx.createHttpClient();
Observable<HttpClientResponse> obs = io.vertx.rxjava.core.RxHelper.get(client, 8080, "localhost", "/foo");
List<Buffer> bodies = Collections.synchronizedList(new ArrayList<>());
CountDownLatch reqLatch = new CountDownLatch(1);
obs.subscribe(resp -> {
resp.toObservable().subscribe(bodies::add, this::fail, reqLatch::countDown);
}, this::fail);
awaitLatch(reqLatch);
obs.subscribe(resp -> {
resp.toObservable().subscribe(bodies::add, this::fail, () -> {
assertEquals(Arrays.asList("request=0", "request=1"), bodies.stream().map(Buffer::toString).collect(Collectors.toList()));
testComplete();
});
}, this::fail);
await();
}
开发者ID:vert-x3,项目名称:vertx-rx,代码行数:28,代码来源:CoreApiTest.java
示例14: doDownload
import io.vertx.rxjava.core.http.HttpClient; //导入依赖的package包/类
/**
* Download a file
* @param downloadUrl the URL to download from
* @param dest the destination file
* @return a single emitting exactly one item once the file has been
* downloaded
*/
private Single<Void> doDownload(String downloadUrl, AsyncFile dest) {
ObservableFuture<Void> observable = RxHelper.observableFuture();
Handler<AsyncResult<Void>> handler = observable.toHandler();
HttpClientOptions options = new HttpClientOptions();
if (downloadUrl.startsWith("https")) {
options.setSsl(true);
}
HttpClient client = vertx.createHttpClient(options);
HttpClientRequest req = client.getAbs(downloadUrl);
req.exceptionHandler(t -> handler.handle(Future.failedFuture(t)));
req.handler(res -> {
if (res.statusCode() != 200) {
handler.handle(Future.failedFuture(new HttpException(res.statusCode(),
res.statusMessage())));
return;
}
// get content-length
int length;
int[] read = { 0 };
int[] lastOutput = { 0 };
String contentLength = res.getHeader("Content-Length");
if (contentLength != null) {
length = Integer.parseInt(contentLength);
} else {
length = 0;
}
res.exceptionHandler(t -> handler.handle(Future.failedFuture(t)));
// download file contents, log progress
res.handler(buf -> {
read[0] += buf.length();
if (lastOutput[0] == 0 || read[0] - lastOutput[0] > 1024 * 2048) {
logProgress(length, read[0]);
lastOutput[0] = read[0];
}
dest.write(buf);
});
res.endHandler(v -> {
logProgress(length, read[0]);
handler.handle(Future.succeededFuture());
});
});
req.end();
return observable.toSingle();
}
开发者ID:georocket,项目名称:georocket,代码行数:61,代码来源:ElasticsearchInstaller.java
注:本文中的io.vertx.rxjava.core.http.HttpClient类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论