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


Java HttpClient类代码示例

本文整理汇总了Java中reactor.ipc.netty.http.client.HttpClient的典型用法代码示例。如果您正苦于以下问题:Java HttpClient类的具体用法?Java HttpClient怎么用?Java HttpClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: errorResponseAndReturn

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void errorResponseAndReturn() throws Exception {
	NettyContext c = HttpServer.create(0)
	                           .newHandler((req, resp) -> Mono.error(new Exception("returnError")))
	                           .block();

	HttpClientResponse res =
			HttpClient.create(c.address().getPort())
			          .get("/return", r -> r.failOnServerError(false))
			          .block();
	assertThat(res.status().code()).isEqualTo(500);
	res.dispose();

	c.dispose();

}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:17,代码来源:HttpServerTests.java

示例2: httpRespondsEmpty

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void httpRespondsEmpty() {
	NettyContext server =
			HttpServer.create(0)
			          .newRouter(r ->
			              r.post("/test/{param}", (req, res) -> Mono.empty()))
			          .block(Duration.ofSeconds(30));

	HttpClient client =
			HttpClient.create("localhost", server.address().getPort());

	Mono<ByteBuf> content = client
			            .post("/test/World", req -> req.header("Content-Type", "text/plain")
			                                           .sendString(Mono.just("Hello")
			                                           .log("client-send")))
			            .flatMap(res -> res.receive()
			                               .log("client-received")
			                               .next())
			            .doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));

	StepVerifier.create(content)
			    .expectComplete()
			    .verify(Duration.ofSeconds(5000));

	server.dispose();
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:27,代码来源:HttpTests.java

示例3: main

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    GuavaCacheMetrics.monitor(registry, guavaCache, "book.guava");

    // read all of Frankenstein
    HttpClient.create("www.gutenberg.org")
        .get("/cache/epub/84/pg84.txt")
        .flatMapMany(res -> res.addHandler(wordDecoder()).receive().asString())
        .delayElements(Duration.ofMillis(10)) // one word per 10 ms
        .filter(word -> !word.isEmpty())
        .doOnNext(word -> {
            if (guavaCache.getIfPresent(word) == null)
                guavaCache.put(word, 1);
        })
        .blockLast();
}
 
开发者ID:micrometer-metrics,项目名称:micrometer,代码行数:17,代码来源:CacheSample.java

示例4: testChannelInactiveThrowsIOException

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void testChannelInactiveThrowsIOException() throws Exception {
	ExecutorService threadPool = Executors.newCachedThreadPool();

	int abortServerPort = SocketUtils.findAvailableTcpPort();
	ConnectionAbortServer abortServer = new ConnectionAbortServer(abortServerPort);

	threadPool.submit(abortServer);

	if(!abortServer.await(10, TimeUnit.SECONDS)){
		throw new IOException("Fail to start test server");
	}

	Mono<HttpClientResponse> response =
			HttpClient.create(ops -> ops.host("localhost")
			                            .port(abortServerPort))
			          .get("/",
					          req -> req.sendHeaders()
					                    .sendString(Flux.just("a", "b", "c")));

	StepVerifier.create(response)
	            .expectError()
	            .verify();

	abortServer.close();
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:27,代码来源:ChannelOperationsHandlerTest.java

示例5: simpleTest

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void simpleTest() {
	httpServer = HttpServer.create(0)
	                       .newHandler((in, out) -> out.sendWebsocket((i, o) -> o.sendString(
			                       Mono.just("test"))))
	                       .block(Duration.ofSeconds(30));

	String res = HttpClient.create(httpServer.address()
	                                  .getPort())
	                .get("/test",
			                out -> out.addHeader("Authorization", auth)
			                          .sendWebsocket())
	                .flatMapMany(in -> in.receive()
	                                 .asString())
	                .log()
	                .collectList()
	                .block(Duration.ofSeconds(30))
	                .get(0);

	Assert.assertThat(res, is("test"));
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:22,代码来源:WebsocketTest.java

示例6: simpleSubprotocolServerNoSubprotocol

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void simpleSubprotocolServerNoSubprotocol() throws Exception {
	httpServer = HttpServer.create(0)
	                                    .newHandler((in, out) -> out.sendWebsocket((i, o) -> o.sendString(
			                                    Mono.just("test"))))
	                                    .block(Duration.ofSeconds(30));

	StepVerifier.create(
			HttpClient.create(
					httpServer.address().getPort())
			          .get("/test",
					          out -> out.addHeader("Authorization", auth)
					                    .sendWebsocket("SUBPROTOCOL,OTHER"))
			          .flatMapMany(in -> in.receive().asString())
	)
	            .verifyErrorMessage("Invalid subprotocol. Actual: null. Expected one of: SUBPROTOCOL,OTHER");
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:18,代码来源:WebsocketTest.java

示例7: simpleSubprotocolServerNotSupported

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void simpleSubprotocolServerNotSupported() throws Exception {
	httpServer = HttpServer.create(0)
	                       .newHandler((in, out) -> out.sendWebsocket(
			                       "protoA,protoB",
			                       (i, o) -> o.sendString(Mono.just("test"))))
	                       .block(Duration.ofSeconds(30));

	StepVerifier.create(
			HttpClient.create(
					httpServer.address().getPort())
			          .get("/test",
					          out -> out.addHeader("Authorization", auth)
					                    .sendWebsocket("SUBPROTOCOL,OTHER"))
			          .flatMapMany(in -> in.receive().asString())
	)
	            //the SERVER returned null which means that it couldn't select a protocol
	            .verifyErrorMessage("Invalid subprotocol. Actual: null. Expected one of: SUBPROTOCOL,OTHER");
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:20,代码来源:WebsocketTest.java

示例8: simpleSubprotocolServerSupported

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void simpleSubprotocolServerSupported() throws Exception {
	httpServer = HttpServer.create(0)
	                       .newHandler((in, out) -> out.sendWebsocket(
			                       "SUBPROTOCOL",
			                       (i, o) -> o.sendString(
					                       Mono.just("test"))))
	                       .block(Duration.ofSeconds(30));

	String res = HttpClient.create(httpServer.address().getPort())
	                       .get("/test",
			                out -> out.addHeader("Authorization", auth)
			                          .sendWebsocket("SUBPROTOCOL,OTHER"))
	                .flatMapMany(in -> in.receive().asString()).log().collectList().block(Duration.ofSeconds(30)).get(0);

	Assert.assertThat(res, is("test"));
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:18,代码来源:WebsocketTest.java

示例9: simpleSubprotocolSelected

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void simpleSubprotocolSelected() throws Exception {
	httpServer = HttpServer.create(0)
	                       .newHandler((in, out) -> out.sendWebsocket(
			                       "NOT, Common",
			                       (i, o) -> o.sendString(
					                       Mono.just("SERVER:" + o.selectedSubprotocol()))))
	                       .block(Duration.ofSeconds(30));

	String res = HttpClient.create(httpServer.address().getPort())
	                       .get("/test",
			                out -> out.addHeader("Authorization", auth)
			                          .sendWebsocket("Common,OTHER"))
	                       .map(HttpClientResponse::receiveWebsocket)
	                       .flatMapMany(in -> in.receive().asString()
			                       .map(srv -> "CLIENT:" + in.selectedSubprotocol() + "-" + srv))
	                       .log().collectList().block(Duration.ofSeconds(30)).get(0);

	Assert.assertThat(res, is("CLIENT:Common-SERVER:Common"));
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:21,代码来源:WebsocketTest.java

示例10: noSubprotocolSelected

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void noSubprotocolSelected() {
	httpServer = HttpServer.create(0)
	                       .newHandler((in, out) -> out.sendWebsocket((i, o) -> o.sendString(
			                       Mono.just("SERVER:" + o.selectedSubprotocol()))))
	                       .block(Duration.ofSeconds(30));

	String res = HttpClient.create(httpServer.address()
	                                         .getPort())
	                       .get("/test",
			                       out -> out.addHeader("Authorization", auth)
			                                 .sendWebsocket())
	                       .map(HttpClientResponse::receiveWebsocket)
	                       .flatMapMany(in -> in.receive()
	                                        .asString()
	                                        .map(srv -> "CLIENT:" + in.selectedSubprotocol() + "-" + srv))
	                       .log()
	                       .collectList()
	                       .block(Duration.ofSeconds(30))
	                       .get(0);

	Assert.assertThat(res, is("CLIENT:null-SERVER:null"));
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:24,代码来源:WebsocketTest.java

示例11: anySubprotocolSelectsFirstClientProvided

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void anySubprotocolSelectsFirstClientProvided() {
	httpServer = HttpServer.create(0)
	                       .newHandler((in, out) -> out.sendWebsocket("proto2,*", (i, o) -> o.sendString(
			                       Mono.just("SERVER:" + o.selectedSubprotocol()))))
	                       .block(Duration.ofSeconds(30));

	String res = HttpClient.create(httpServer.address()
	                                         .getPort())
	                       .get("/test",
			                       out -> out.addHeader("Authorization", auth)
			                                 .sendWebsocket("proto1, proto2"))
	                       .map(HttpClientResponse::receiveWebsocket)
	                       .flatMapMany(in -> in.receive()
	                                        .asString()
	                                        .map(srv -> "CLIENT:" + in.selectedSubprotocol() + "-" + srv))
	                       .log()
	                       .collectList()
	                       .block(Duration.ofSeconds(30))
	                       .get(0);

	Assert.assertThat(res, is("CLIENT:proto1-SERVER:proto1"));
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:24,代码来源:WebsocketTest.java

示例12: assertSendFile

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
private void assertSendFile(Function<HttpServerResponse, NettyOutbound> fn) {
	NettyContext context =
			HttpServer.create(opt -> opt.host("localhost"))
			          .newHandler((req, resp) -> fn.apply(resp))
			          .block();


	HttpClientResponse response =
			HttpClient.create(opt -> opt.connectAddress(() -> context.address()))
			          .get("/foo")
			          .block(Duration.ofSeconds(120));

	context.dispose();
	context.onClose().block();

	String body = response.receive().aggregate().asString(StandardCharsets.UTF_8).block();

	assertThat(body)
			.startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.")
			.contains("1024 mark here -><- 1024 mark here")
			.endsWith("End of File");
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:23,代码来源:HttpServerTests.java

示例13: flushOnComplete

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void flushOnComplete() {

	Flux<String> test = Flux.range(0, 100)
	                        .map(n -> String.format("%010d", n));

	NettyContext c = HttpServer.create(0)
	                           .newHandler((req, resp) -> resp.sendString(test.map(s -> s + "\n")))
	                           .block(Duration.ofSeconds(30));

	Flux<String> client = HttpClient.create(c.address()
	                                         .getPort())
	                                .get("/")
	                                .block(Duration.ofSeconds(30))
	                                .addHandler(new LineBasedFrameDecoder(10))
	                                .receive()
	                                .asString();

	StepVerifier.create(client)
	            .expectNextSequence(test.toIterable())
	            .expectComplete()
	            .verify(Duration.ofSeconds(30));

	c.dispose();
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:26,代码来源:HttpServerTests.java

示例14: startRouter

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void startRouter() {
	BlockingNettyContext facade = HttpServer.create(0)
	                                        .startRouter(routes -> routes.get("/hello",
			                                        (req, resp) -> resp.sendString(Mono.just("hello!"))));

	try {
		HttpClientResponse res =
				HttpClient.create(facade.getPort())
				          .get("/hello")
				          .block();
		assertThat(res.status().code()).isEqualTo(200);
		res.dispose();

		res = HttpClient.create(facade.getPort())
		                .get("/helloMan", req -> req.failOnClientError(false))
		                .block();
		assertThat(res.status().code()).isEqualTo(404);
		res.dispose();
	}
	finally {
		facade.shutdown();
	}
}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:25,代码来源:HttpServerTests.java

示例15: testIssue186

import reactor.ipc.netty.http.client.HttpClient; //导入依赖的package包/类
@Test
public void testIssue186() {
	NettyContext server =
			HttpServer.create(0)
			          .newHandler((req, res) -> res.status(200).send())
			          .block(Duration.ofSeconds(300));

	HttpClient client =
			HttpClient.create(ops -> ops.connectAddress(() -> server.address())
					                    .poolResources(PoolResources.fixed("test", 1)));

	try {
		doTestIssue186(client);
		doTestIssue186(client);
	}
	finally {
		server.dispose();
	}

}
 
开发者ID:reactor,项目名称:reactor-netty,代码行数:21,代码来源:HttpServerTests.java


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