1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.examples;
28
29 import java.util.concurrent.Future;
30
31 import javax.net.ssl.SSLPeerUnverifiedException;
32 import javax.net.ssl.SSLSession;
33
34 import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
35 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
36 import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
37 import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
38 import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
39 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
40 import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
41 import org.apache.hc.client5.http.protocol.HttpClientContext;
42 import org.apache.hc.core5.concurrent.FutureCallback;
43 import org.apache.hc.core5.http.HttpHost;
44 import org.apache.hc.core5.http.message.StatusLine;
45 import org.apache.hc.core5.io.CloseMode;
46
47
48
49
50
51 public class AsyncClientSNI {
52
53 public static void main(final String[] args) throws Exception {
54 try (final CloseableHttpAsyncClient client = HttpAsyncClients.createSystem()) {
55
56 client.start();
57
58 final HttpHost target = new HttpHost("https", "www.google.com");
59 final SimpleHttpRequest request = SimpleRequestBuilder.get()
60 .setUri("https://www.google.ch/")
61 .build();
62
63 final HttpClientContext clientContext = HttpClientContext.create();
64
65 System.out.println("Executing request " + request);
66 final Future<SimpleHttpResponse> future = client.execute(
67 target,
68 SimpleRequestProducer.create(request),
69 SimpleResponseConsumer.create(),
70 null,
71 clientContext,
72 new FutureCallback<SimpleHttpResponse>() {
73
74 @Override
75 public void completed(final SimpleHttpResponse response) {
76 System.out.println(request + "->" + new StatusLine(response));
77 final SSLSession sslSession = clientContext.getSSLSession();
78 if (sslSession != null) {
79 try {
80 System.out.println("Peer: " + sslSession.getPeerPrincipal());
81 System.out.println("TLS protocol: " + sslSession.getProtocol());
82 System.out.println("TLS cipher suite: " + sslSession.getCipherSuite());
83 } catch (final SSLPeerUnverifiedException ignore) {
84 }
85 }
86 }
87
88 @Override
89 public void failed(final Exception ex) {
90 System.out.println(request + "->" + ex);
91 }
92
93 @Override
94 public void cancelled() {
95 System.out.println(request + " cancelled");
96 }
97
98 });
99 future.get();
100
101 System.out.println("Shutting down");
102 client.close(CloseMode.GRACEFUL);
103 }
104 }
105
106 }