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 org.apache.hc.client5.http.config.ConnectionConfig;
30  import org.apache.hc.client5.http.config.TlsConfig;
31  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
32  import org.apache.hc.client5.http.impl.classic.HttpClients;
33  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
34  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
35  import org.apache.hc.core5.http.ClassicHttpRequest;
36  import org.apache.hc.core5.http.HttpHost;
37  import org.apache.hc.core5.http.URIScheme;
38  import org.apache.hc.core5.http.io.entity.EntityUtils;
39  import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
40  import org.apache.hc.core5.http.message.StatusLine;
41  import org.apache.hc.core5.http.ssl.TLS;
42  import org.apache.hc.core5.util.TimeValue;
43  import org.apache.hc.core5.util.Timeout;
44  
45  /**
46   * This example demonstrates how to use connection configuration on a per-route or a per-host
47   * basis.
48   */
49  public class ClientConnectionConfig {
50  
51      public final static void main(final String[] args) throws Exception {
52          final PoolingHttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
53                  .setConnectionConfigResolver(route -> {
54                      // Use different settings for all secure (TLS) connections
55                      if (route.isSecure()) {
56                          return ConnectionConfig.custom()
57                                  .setConnectTimeout(Timeout.ofMinutes(2))
58                                  .setSocketTimeout(Timeout.ofMinutes(2))
59                                  .setValidateAfterInactivity(TimeValue.ofMinutes(1))
60                                  .setTimeToLive(TimeValue.ofHours(1))
61                                  .build();
62                      }
63                      return ConnectionConfig.custom()
64                              .setConnectTimeout(Timeout.ofMinutes(1))
65                              .setSocketTimeout(Timeout.ofMinutes(1))
66                              .setValidateAfterInactivity(TimeValue.ofSeconds(15))
67                              .setTimeToLive(TimeValue.ofMinutes(15))
68                              .build();
69                  })
70                  .setTlsConfigResolver(host -> {
71                      // Use different settings for specific hosts
72                      if (host.getSchemeName().equalsIgnoreCase("httpbin.org")) {
73                          return TlsConfig.custom()
74                                  .setSupportedProtocols(TLS.V_1_3)
75                                  .setHandshakeTimeout(Timeout.ofSeconds(10))
76                                  .build();
77                      }
78                      return TlsConfig.DEFAULT;
79                  })
80                  .build();
81          try (CloseableHttpClient httpclient = HttpClients.custom()
82                  .setConnectionManager(cm)
83                  .build()) {
84  
85              for (final URIScheme uriScheme : URIScheme.values()) {
86                  final ClassicHttpRequest request = ClassicRequestBuilder.get()
87                          .setHttpHost(new HttpHost(uriScheme.id, "httpbin.org"))
88                          .setPath("/headers")
89                          .build();
90                  System.out.println("Executing request " + request);
91                  httpclient.execute(request, response -> {
92                      System.out.println("----------------------------------------");
93                      System.out.println(request + "->" + new StatusLine(response));
94                      EntityUtils.consume(response.getEntity());
95                      return null;
96                  });
97              }
98          }
99      }
100 
101 }