View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * This example demonstrates how to use SNI to send requests to a virtual HTTPS
49   * endpoint using the async I/O.
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 }