Improved async client examples
This commit is contained in:
parent
1174c240e2
commit
567b53d4b1
|
@ -29,14 +29,17 @@ package org.apache.hc.client5.http.examples;
|
|||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
|
||||
import org.apache.hc.client5.http.auth.AuthScope;
|
||||
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
|
||||
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
|
||||
/**
|
||||
|
@ -55,28 +58,29 @@ public class AsyncClientAuthentication {
|
|||
.build();
|
||||
httpclient.start();
|
||||
|
||||
final String requestUri = "http://httpbin.org/basic-auth/user/passwd";
|
||||
final SimpleHttpRequest httpget = SimpleHttpRequests.get(requestUri);
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get("http://httpbin.org/basic-auth/user/passwd")
|
||||
.build();
|
||||
|
||||
System.out.println("Executing request " + requestUri);
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<SimpleHttpResponse> future = httpclient.execute(
|
||||
httpget,
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
new FutureCallback<SimpleHttpResponse>() {
|
||||
|
||||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
System.out.println(requestUri + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -30,14 +30,15 @@ import java.util.concurrent.Future;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
|
||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.reactor.IOReactorConfig;
|
||||
import org.apache.hc.core5.util.TimeValue;
|
||||
|
@ -63,9 +64,12 @@ public class AsyncClientConnectionEviction {
|
|||
|
||||
client.start();
|
||||
|
||||
final HttpHost target = new HttpHost("httpbin.org");
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||
.setHttpHost(new HttpHost("httpbin.org"))
|
||||
.setPath("/")
|
||||
.build();
|
||||
|
||||
final SimpleHttpRequest request = SimpleHttpRequests.get(target, "/");
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<SimpleHttpResponse> future1 = client.execute(
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
|
@ -73,18 +77,18 @@ public class AsyncClientConnectionEviction {
|
|||
|
||||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
System.out.println(request.getRequestUri() + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
System.out.println(request.getRequestUri() + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println(request.getRequestUri() + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -96,24 +100,23 @@ public class AsyncClientConnectionEviction {
|
|||
// Previous connection should get evicted from the pool by now
|
||||
|
||||
final Future<SimpleHttpResponse> future2 = client.execute(
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
request,
|
||||
new FutureCallback<SimpleHttpResponse>() {
|
||||
|
||||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
System.out.println(request.getRequestUri() + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
System.out.println(request.getRequestUri() + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println(request.getRequestUri() + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -34,8 +34,8 @@ import javax.net.ssl.SSLContext;
|
|||
import javax.net.ssl.SSLSession;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
|
||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||
|
@ -46,6 +46,7 @@ import org.apache.hc.client5.http.protocol.HttpClientContext;
|
|||
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.ssl.SSLContexts;
|
||||
|
@ -97,11 +98,15 @@ public class AsyncClientCustomSSL {
|
|||
|
||||
client.start();
|
||||
|
||||
final HttpHost target = new HttpHost("https", "httpbin.org", 443);
|
||||
final String requestUri = "/";
|
||||
final HttpHost target = new HttpHost("https", "httpbin.org");
|
||||
final HttpClientContext clientContext = HttpClientContext.create();
|
||||
|
||||
final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||
.setHttpHost(target)
|
||||
.setPath("/")
|
||||
.build();
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<SimpleHttpResponse> future = client.execute(
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
|
@ -110,23 +115,23 @@ public class AsyncClientCustomSSL {
|
|||
|
||||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(response.getBody());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
final SSLSession sslSession = clientContext.getSSLSession();
|
||||
if (sslSession != null) {
|
||||
System.out.println("SSL protocol " + sslSession.getProtocol());
|
||||
System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
|
||||
}
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
System.out.println(requestUri + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -27,7 +27,6 @@
|
|||
package org.apache.hc.client5.http.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
@ -41,6 +40,8 @@ import org.apache.hc.core5.http.Header;
|
|||
import org.apache.hc.core5.http.HttpException;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.config.Http1Config;
|
||||
import org.apache.hc.core5.http.message.BasicHttpRequest;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
|
||||
import org.apache.hc.core5.http.nio.CapacityChannel;
|
||||
import org.apache.hc.core5.http.nio.DataStreamChannel;
|
||||
|
@ -50,6 +51,7 @@ import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
|
|||
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
|
||||
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.http.support.BasicRequestBuilder;
|
||||
import org.apache.hc.core5.http2.HttpVersionPolicy;
|
||||
import org.apache.hc.core5.http2.config.H2Config;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
|
@ -75,12 +77,13 @@ public class AsyncClientFullDuplexExchange {
|
|||
|
||||
client.start();
|
||||
|
||||
final URI requestUri = new URI("http://httpbin.org/post");
|
||||
final BasicRequestProducer requestProducer = new BasicRequestProducer(
|
||||
"POST", requestUri, new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
|
||||
final BasicHttpRequest request = BasicRequestBuilder.post("http://httpbin.org/post").build();
|
||||
final BasicRequestProducer requestProducer = new BasicRequestProducer(request,
|
||||
new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
|
||||
final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(
|
||||
new StringAsyncEntityConsumer());
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
client.execute(new AsyncClientExchangeHandler() {
|
||||
|
||||
|
@ -93,12 +96,12 @@ public class AsyncClientFullDuplexExchange {
|
|||
|
||||
@Override
|
||||
public void cancel() {
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception cause) {
|
||||
System.out.println(requestUri + "->" + cause);
|
||||
System.out.println(request + "->" + cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -120,7 +123,7 @@ public class AsyncClientFullDuplexExchange {
|
|||
public void consumeInformation(
|
||||
final HttpResponse response,
|
||||
final HttpContext context) throws HttpException, IOException {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -128,7 +131,7 @@ public class AsyncClientFullDuplexExchange {
|
|||
final HttpResponse response,
|
||||
final EntityDetails entityDetails,
|
||||
final HttpContext context) throws HttpException, IOException {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
responseConsumer.consumeResponse(response, entityDetails, context, null);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,6 @@
|
|||
package org.apache.hc.client5.http.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
@ -40,6 +39,8 @@ import org.apache.hc.core5.http.EntityDetails;
|
|||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpException;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.message.BasicHttpRequest;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
|
||||
import org.apache.hc.core5.http.nio.CapacityChannel;
|
||||
import org.apache.hc.core5.http.nio.DataStreamChannel;
|
||||
|
@ -49,6 +50,7 @@ import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
|
|||
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
|
||||
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.http.support.BasicRequestBuilder;
|
||||
import org.apache.hc.core5.http2.HttpVersionPolicy;
|
||||
import org.apache.hc.core5.http2.config.H2Config;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
|
@ -71,12 +73,13 @@ public class AsyncClientH2FullDuplexExchange {
|
|||
|
||||
client.start();
|
||||
|
||||
final URI requestUri = new URI("http://nghttp2.org/httpbin/post");
|
||||
final BasicRequestProducer requestProducer = new BasicRequestProducer(
|
||||
"POST", requestUri, new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
|
||||
final BasicHttpRequest request = BasicRequestBuilder.post("https://nghttp2.org/httpbin/post").build();
|
||||
final BasicRequestProducer requestProducer = new BasicRequestProducer(request,
|
||||
new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
|
||||
final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(
|
||||
new StringAsyncEntityConsumer());
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
client.execute(new AsyncClientExchangeHandler() {
|
||||
|
||||
|
@ -89,12 +92,12 @@ public class AsyncClientH2FullDuplexExchange {
|
|||
|
||||
@Override
|
||||
public void cancel() {
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception cause) {
|
||||
System.out.println(requestUri + "->" + cause);
|
||||
System.out.println(request + "->" + cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -116,7 +119,7 @@ public class AsyncClientH2FullDuplexExchange {
|
|||
public void consumeInformation(
|
||||
final HttpResponse response,
|
||||
final HttpContext context) throws HttpException, IOException {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -124,7 +127,7 @@ public class AsyncClientH2FullDuplexExchange {
|
|||
final HttpResponse response,
|
||||
final EntityDetails entityDetails,
|
||||
final HttpContext context) throws HttpException, IOException {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
responseConsumer.consumeResponse(response, entityDetails, context, null);
|
||||
}
|
||||
|
||||
|
|
|
@ -31,14 +31,15 @@ import java.util.concurrent.Future;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
|
||||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
|
||||
import org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
|
||||
import org.apache.hc.core5.http2.HttpVersionPolicy;
|
||||
import org.apache.hc.core5.http2.config.H2Config;
|
||||
|
@ -63,7 +64,7 @@ public class AsyncClientH2Multiplexing {
|
|||
|
||||
client.start();
|
||||
|
||||
final HttpHost target = new HttpHost("nghttp2.org");
|
||||
final HttpHost target = new HttpHost("https", "nghttp2.org");
|
||||
final Future<AsyncClientEndpoint> leaseFuture = client.lease(target, null);
|
||||
final AsyncClientEndpoint endpoint = leaseFuture.get(30, TimeUnit.SECONDS);
|
||||
try {
|
||||
|
@ -71,7 +72,12 @@ public class AsyncClientH2Multiplexing {
|
|||
|
||||
final CountDownLatch latch = new CountDownLatch(requestUris.length);
|
||||
for (final String requestUri: requestUris) {
|
||||
final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||
.setHttpHost(target)
|
||||
.setPath(requestUri)
|
||||
.build();
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
endpoint.execute(
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
|
@ -80,20 +86,20 @@ public class AsyncClientH2Multiplexing {
|
|||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
latch.countDown();
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
latch.countDown();
|
||||
System.out.println(requestUri + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
latch.countDown();
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -38,13 +38,13 @@ import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
|
|||
import org.apache.hc.core5.function.Supplier;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpException;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.HttpRequest;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.Method;
|
||||
import org.apache.hc.core5.http.message.BasicHttpRequest;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
|
||||
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
|
||||
import org.apache.hc.core5.http.support.BasicRequestBuilder;
|
||||
import org.apache.hc.core5.http2.HttpVersionPolicy;
|
||||
import org.apache.hc.core5.http2.config.H2Config;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
|
@ -115,17 +115,18 @@ public class AsyncClientH2ServerPush {
|
|||
|
||||
});
|
||||
|
||||
final HttpHost target = new HttpHost("nghttp2.org");
|
||||
final String requestURI = "/httpbin/";
|
||||
final BasicHttpRequest request = BasicRequestBuilder.get("https://nghttp2.org/httpbin/").build();
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<Void> future = client.execute(
|
||||
new BasicRequestProducer(Method.GET, target, requestURI),
|
||||
new BasicRequestProducer(request, null),
|
||||
new AbstractCharResponseConsumer<Void>() {
|
||||
|
||||
@Override
|
||||
protected void start(
|
||||
final HttpResponse response,
|
||||
final ContentType contentType) throws HttpException, IOException {
|
||||
System.out.println(requestURI + "->" + new StatusLine(response));
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -144,7 +145,7 @@ public class AsyncClientH2ServerPush {
|
|||
|
||||
@Override
|
||||
public void failed(final Exception cause) {
|
||||
System.out.println(requestURI + "->" + cause);
|
||||
System.out.println(request + "->" + cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -31,8 +31,8 @@ import java.util.concurrent.Future;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
|
||||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
|
||||
|
@ -40,6 +40,7 @@ import org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient;
|
|||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.config.Http1Config;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
|
||||
import org.apache.hc.core5.http2.HttpVersionPolicy;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
|
@ -70,7 +71,12 @@ public class AsyncClientHttp1Pipelining {
|
|||
|
||||
final CountDownLatch latch = new CountDownLatch(requestUris.length);
|
||||
for (final String requestUri: requestUris) {
|
||||
final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||
.setHttpHost(target)
|
||||
.setPath(requestUri)
|
||||
.build();
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
endpoint.execute(
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
|
@ -79,20 +85,20 @@ public class AsyncClientHttp1Pipelining {
|
|||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
latch.countDown();
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
latch.countDown();
|
||||
System.out.println(requestUri + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
latch.countDown();
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -29,12 +29,15 @@ package org.apache.hc.client5.http.examples;
|
|||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
|
||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.reactor.IOReactorConfig;
|
||||
import org.apache.hc.core5.util.Timeout;
|
||||
|
@ -60,26 +63,31 @@ public class AsyncClientHttpExchange {
|
|||
final String[] requestUris = new String[] {"/", "/ip", "/user-agent", "/headers"};
|
||||
|
||||
for (final String requestUri: requestUris) {
|
||||
final SimpleHttpRequest httpget = SimpleHttpRequests.get(target, requestUri);
|
||||
System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||
.setHttpHost(target)
|
||||
.setPath(requestUri)
|
||||
.build();
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<SimpleHttpResponse> future = client.execute(
|
||||
httpget,
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
new FutureCallback<SimpleHttpResponse>() {
|
||||
|
||||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
System.out.println(requestUri + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -37,9 +37,10 @@ import org.apache.hc.core5.http.ContentType;
|
|||
import org.apache.hc.core5.http.HttpException;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.Method;
|
||||
import org.apache.hc.core5.http.message.BasicHttpRequest;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
|
||||
import org.apache.hc.core5.http.support.BasicRequestBuilder;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.reactor.IOReactorConfig;
|
||||
import org.apache.hc.core5.util.Timeout;
|
||||
|
@ -65,15 +66,22 @@ public class AsyncClientHttpExchangeStreaming {
|
|||
final String[] requestUris = new String[] {"/", "/ip", "/user-agent", "/headers"};
|
||||
|
||||
for (final String requestUri: requestUris) {
|
||||
|
||||
final BasicHttpRequest request = BasicRequestBuilder.get()
|
||||
.setHttpHost(target)
|
||||
.setPath(requestUri)
|
||||
.build();
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<Void> future = client.execute(
|
||||
new BasicRequestProducer(Method.GET, target, requestUri),
|
||||
new BasicRequestProducer(request, null),
|
||||
new AbstractCharResponseConsumer<Void>() {
|
||||
|
||||
@Override
|
||||
protected void start(
|
||||
final HttpResponse response,
|
||||
final ContentType contentType) throws HttpException, IOException {
|
||||
System.out.println(requestUri + "->" + new StatusLine(response));
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -98,7 +106,7 @@ public class AsyncClientHttpExchangeStreaming {
|
|||
|
||||
@Override
|
||||
public void failed(final Exception cause) {
|
||||
System.out.println(requestUri + "->" + cause);
|
||||
System.out.println(request + "->" + cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -37,8 +37,8 @@ import org.apache.hc.client5.http.async.AsyncExecCallback;
|
|||
import org.apache.hc.client5.http.async.AsyncExecChain;
|
||||
import org.apache.hc.client5.http.async.AsyncExecChainHandler;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.impl.ChainElement;
|
||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
|
||||
|
@ -53,6 +53,7 @@ import org.apache.hc.core5.http.HttpResponse;
|
|||
import org.apache.hc.core5.http.HttpStatus;
|
||||
import org.apache.hc.core5.http.impl.BasicEntityDetails;
|
||||
import org.apache.hc.core5.http.message.BasicHttpResponse;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
|
||||
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
|
@ -123,26 +124,26 @@ public class AsyncClientInterceptors {
|
|||
|
||||
final String requestUri = "http://httpbin.org/get";
|
||||
for (int i = 0; i < 20; i++) {
|
||||
final SimpleHttpRequest httpget = SimpleHttpRequests.get(requestUri);
|
||||
System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get(requestUri).build();
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<SimpleHttpResponse> future = client.execute(
|
||||
httpget,
|
||||
request,
|
||||
new FutureCallback<SimpleHttpResponse>() {
|
||||
|
||||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
System.out.println(requestUri + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -33,7 +33,10 @@ import java.util.concurrent.Future;
|
|||
import org.apache.hc.client5.http.async.AsyncExecCallback;
|
||||
import org.apache.hc.client5.http.async.AsyncExecChain;
|
||||
import org.apache.hc.client5.http.async.AsyncExecChainHandler;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
|
||||
import org.apache.hc.client5.http.impl.ChainElement;
|
||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||
|
@ -42,11 +45,9 @@ import org.apache.hc.core5.concurrent.FutureCallback;
|
|||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpException;
|
||||
import org.apache.hc.core5.http.HttpRequest;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
|
||||
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
|
||||
import org.apache.hc.core5.http.nio.entity.DigestingEntityProducer;
|
||||
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
|
||||
import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.reactor.IOReactorConfig;
|
||||
import org.apache.hc.core5.util.Timeout;
|
||||
|
@ -87,29 +88,30 @@ public class AsyncClientMessageTrailers {
|
|||
|
||||
client.start();
|
||||
|
||||
final String requestUri = "http://httpbin.org/post";
|
||||
final AsyncRequestProducer requestProducer = AsyncRequestBuilder.post(requestUri)
|
||||
.setEntity(new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN))
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.post("http://httpbin.org/post")
|
||||
.setBody("some stuff", ContentType.TEXT_PLAIN)
|
||||
.build();
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<SimpleHttpResponse> future = client.execute(
|
||||
requestProducer,
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
new FutureCallback<SimpleHttpResponse>() {
|
||||
|
||||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
System.out.println(requestUri + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -31,8 +31,8 @@ import java.util.concurrent.Future;
|
|||
import javax.net.ssl.SSLSession;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
|
||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||
|
@ -43,6 +43,7 @@ import org.apache.hc.client5.http.protocol.HttpClientContext;
|
|||
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.message.StatusLine;
|
||||
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
|
||||
import org.apache.hc.core5.http2.HttpVersionPolicy;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
|
@ -79,11 +80,15 @@ public class AsyncClientTlsAlpn {
|
|||
|
||||
client.start();
|
||||
|
||||
final HttpHost target = new HttpHost("https", "nghttp2.org", 443);
|
||||
final String requestUri = "/httpbin/";
|
||||
final HttpHost target = new HttpHost("https", "nghttp2.org");
|
||||
final HttpClientContext clientContext = HttpClientContext.create();
|
||||
|
||||
final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||
.setHttpHost(target)
|
||||
.setPath("/httpbin/")
|
||||
.build();
|
||||
|
||||
System.out.println("Executing request " + request);
|
||||
final Future<SimpleHttpResponse> future = client.execute(
|
||||
SimpleRequestProducer.create(request),
|
||||
SimpleResponseConsumer.create(),
|
||||
|
@ -92,23 +97,23 @@ public class AsyncClientTlsAlpn {
|
|||
|
||||
@Override
|
||||
public void completed(final SimpleHttpResponse response) {
|
||||
System.out.println(requestUri + "->" + response.getCode());
|
||||
System.out.println(response.getBody());
|
||||
System.out.println(request + "->" + new StatusLine(response));
|
||||
final SSLSession sslSession = clientContext.getSSLSession();
|
||||
if (sslSession != null) {
|
||||
System.out.println("SSL protocol " + sslSession.getProtocol());
|
||||
System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
|
||||
}
|
||||
System.out.println(response.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
System.out.println(requestUri + "->" + ex);
|
||||
System.out.println(request + "->" + ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println(requestUri + " cancelled");
|
||||
System.out.println(request + " cancelled");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue