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.net.URL;
30  
31  import org.apache.hc.client5.http.classic.methods.HttpGet;
32  import org.apache.hc.client5.http.cookie.StandardCookieSpec;
33  import org.apache.hc.client5.http.cookie.CookieSpecFactory;
34  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
35  import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
36  import org.apache.hc.client5.http.impl.classic.HttpClients;
37  import org.apache.hc.client5.http.impl.cookie.RFC6265CookieSpecFactory;
38  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
39  import org.apache.hc.client5.http.io.HttpClientConnectionManager;
40  import org.apache.hc.client5.http.psl.PublicSuffixMatcher;
41  import org.apache.hc.client5.http.psl.PublicSuffixMatcherLoader;
42  import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
43  import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
44  import org.apache.hc.core5.http.config.Lookup;
45  import org.apache.hc.core5.http.config.RegistryBuilder;
46  import org.apache.hc.core5.http.io.entity.EntityUtils;
47  import org.apache.hc.core5.ssl.SSLContexts;
48  
49  /**
50   * This example demonstrates how to use a custom public suffix list.
51   */
52  public class ClientCustomPublicSuffixList {
53  
54      public static void main(final String[] args) throws Exception {
55  
56          // Use PublicSuffixMatcherLoader to load public suffix list from a file,
57          // resource or from an arbitrary URL
58          final PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(
59                  new URL("https://publicsuffix.org/list/effective_tld_names.dat"));
60  
61          // Please use the publicsuffix.org URL to download the list no more than once per day !!!
62          // Please consider making a local copy !!!
63  
64          final RFC6265CookieSpecFactory cookieSpecFactory = new RFC6265CookieSpecFactory(publicSuffixMatcher);
65          final Lookup<CookieSpecFactory> cookieSpecRegistry = RegistryBuilder.<CookieSpecFactory>create()
66                  .register(StandardCookieSpec.RELAXED, cookieSpecFactory)
67                  .register(StandardCookieSpec.STRICT, cookieSpecFactory)
68                  .build();
69          final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
70                  SSLContexts.createDefault(),
71                  new DefaultHostnameVerifier(publicSuffixMatcher));
72          final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
73                  .setSSLSocketFactory(sslsf)
74                  .build();
75          try (final CloseableHttpClient httpclient = HttpClients.custom()
76                  .setConnectionManager(cm)
77                  .setDefaultCookieSpecRegistry(cookieSpecRegistry)
78                  .build()) {
79  
80              final HttpGet httpget = new HttpGet("https://httpbin.org/get");
81  
82              System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
83  
84              try (final CloseableHttpResponse response = httpclient.execute(httpget)) {
85                  System.out.println("----------------------------------------");
86                  System.out.println(response.getCode() + " " + response.getReasonPhrase());
87                  System.out.println(EntityUtils.toString(response.getEntity()));
88              }
89          }
90      }
91  
92  }