Search in sources :

Example 6 with HttpClientConnectionManager

use of org.apache.hc.client5.http.io.HttpClientConnectionManager in project mercury by yellow013.

the class ClientCustomPublicSuffixList method main.

public static void main(final String[] args) throws Exception {
    // Use PublicSuffixMatcherLoader to load public suffix list from a file,
    // resource or from an arbitrary URL
    final PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL("https://publicsuffix.org/list/effective_tld_names.dat"));
    // Please use the publicsuffix.org URL to download the list no more than once
    // per day !!!
    // Please consider making a local copy !!!
    final RFC6265CookieSpecFactory cookieSpecFactory = new RFC6265CookieSpecFactory(publicSuffixMatcher);
    final Lookup<CookieSpecFactory> cookieSpecRegistry = RegistryBuilder.<CookieSpecFactory>create().register(StandardCookieSpec.RELAXED, cookieSpecFactory).register(StandardCookieSpec.STRICT, cookieSpecFactory).build();
    final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault(), new DefaultHostnameVerifier(publicSuffixMatcher));
    final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(sslsf).build();
    try (final CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).setDefaultCookieSpecRegistry(cookieSpecRegistry).build()) {
        final HttpGet httpget = new HttpGet("https://httpbin.org/get");
        System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
        try (final CloseableHttpResponse response = httpclient.execute(httpget)) {
            System.out.println("----------------------------------------");
            System.out.println(response.getCode() + " " + response.getReasonPhrase());
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : RFC6265CookieSpecFactory(org.apache.hc.client5.http.impl.cookie.RFC6265CookieSpecFactory) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) RFC6265CookieSpecFactory(org.apache.hc.client5.http.impl.cookie.RFC6265CookieSpecFactory) CookieSpecFactory(org.apache.hc.client5.http.cookie.CookieSpecFactory) DefaultHostnameVerifier(org.apache.hc.client5.http.ssl.DefaultHostnameVerifier) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) PublicSuffixMatcher(org.apache.hc.client5.http.psl.PublicSuffixMatcher) HttpClientConnectionManager(org.apache.hc.client5.http.io.HttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) URL(java.net.URL)

Example 7 with HttpClientConnectionManager

use of org.apache.hc.client5.http.io.HttpClientConnectionManager in project geo-platform by geosdi.

the class GPServerProxy method createClientConnectionManager.

/**
 * @return {@link HttpClientConnectionManager}
 */
HttpClientConnectionManager createClientConnectionManager() {
    try {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, (chain, authType) -> true);
        SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSF).build());
        cm.setMaxTotal(10);
        cm.setDefaultMaxPerRoute(3);
        return cm;
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IllegalStateException(ex);
    }
}
Also used : ConnectionSocketFactory(org.apache.hc.client5.http.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.hc.client5.http.socket.PlainConnectionSocketFactory) SSLContextBuilder(org.apache.hc.core5.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) ServletException(javax.servlet.ServletException) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Example 8 with HttpClientConnectionManager

use of org.apache.hc.client5.http.io.HttpClientConnectionManager in project mercury by yellow013.

the class ClientWithRequestFuture method main.

public static void main(final String[] args) throws Exception {
    // the simplest way to create a HttpAsyncClientWithFuture
    final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal(5).build();
    final CloseableHttpClient httpclient = HttpClientBuilder.create().setConnectionManager(cm).build();
    final ExecutorService execService = Executors.newFixedThreadPool(5);
    try (final FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(httpclient, execService)) {
        // Because things are asynchronous, you must provide a HttpClientResponseHandler
        final HttpClientResponseHandler<Boolean> handler = new HttpClientResponseHandler<Boolean>() {

            @Override
            public Boolean handleResponse(final ClassicHttpResponse response) throws IOException {
                // simply return true if the status was OK
                return response.getCode() == HttpStatus.SC_OK;
            }
        };
        // Simple request ...
        final HttpGet request1 = new HttpGet("http://httpbin.org/get");
        final FutureTask<Boolean> futureTask1 = requestExecService.execute(request1, HttpClientContext.create(), handler);
        final Boolean wasItOk1 = futureTask1.get();
        System.out.println("It was ok? " + wasItOk1);
        // Cancel a request
        try {
            final HttpGet request2 = new HttpGet("http://httpbin.org/get");
            final FutureTask<Boolean> futureTask2 = requestExecService.execute(request2, HttpClientContext.create(), handler);
            futureTask2.cancel(true);
            final Boolean wasItOk2 = futureTask2.get();
            System.out.println("It was cancelled so it should never print this: " + wasItOk2);
        } catch (final CancellationException e) {
            System.out.println("We cancelled it, so this is expected");
        }
        // Request with a timeout
        final HttpGet request3 = new HttpGet("http://httpbin.org/get");
        final FutureTask<Boolean> futureTask3 = requestExecService.execute(request3, HttpClientContext.create(), handler);
        final Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
        System.out.println("It was ok? " + wasItOk3);
        final FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {

            @Override
            public void completed(final Boolean result) {
                System.out.println("completed with " + result);
            }

            @Override
            public void failed(final Exception ex) {
                System.out.println("failed with " + ex.getMessage());
            }

            @Override
            public void cancelled() {
                System.out.println("cancelled");
            }
        };
        // Simple request with a callback
        final HttpGet request4 = new HttpGet("http://httpbin.org/get");
        // using a null HttpContext here since it is optional
        // the callback will be called when the task completes, fails, or is cancelled
        final FutureTask<Boolean> futureTask4 = requestExecService.execute(request4, HttpClientContext.create(), handler, callback);
        final Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
        System.out.println("It was ok? " + wasItOk4);
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) HttpClientResponseHandler(org.apache.hc.core5.http.io.HttpClientResponseHandler) FutureRequestExecutionService(org.apache.hc.client5.http.impl.classic.FutureRequestExecutionService) CancellationException(java.util.concurrent.CancellationException) ExecutorService(java.util.concurrent.ExecutorService) HttpClientConnectionManager(org.apache.hc.client5.http.io.HttpClientConnectionManager) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 9 with HttpClientConnectionManager

use of org.apache.hc.client5.http.io.HttpClientConnectionManager in project mercury by yellow013.

the class ClientCustomSSL method main.

public static final void main(final String[] args) throws Exception {
    // Trust standard CA and those trusted by our custom strategy
    final SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new TrustStrategy() {

        @Override
        public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
            final X509Certificate cert = chain[0];
            return "CN=httpbin.org".equalsIgnoreCase(cert.getSubjectDN().getName());
        }
    }).build();
    // Allow TLSv1.2 protocol only
    final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create().setSslContext(sslcontext).setTlsVersions(TLS.V_1_2).build();
    final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(sslSocketFactory).build();
    try (CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build()) {
        final HttpGet httpget = new HttpGet("https://httpbin.org/");
        System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
        final HttpClientContext clientContext = HttpClientContext.create();
        try (CloseableHttpResponse response = httpclient.execute(httpget, clientContext)) {
            System.out.println("----------------------------------------");
            System.out.println(response.getCode() + " " + response.getReasonPhrase());
            System.out.println(EntityUtils.toString(response.getEntity()));
            final SSLSession sslSession = clientContext.getSSLSession();
            if (sslSession != null) {
                System.out.println("SSL protocol " + sslSession.getProtocol());
                System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) TrustStrategy(org.apache.hc.core5.ssl.TrustStrategy) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) SSLSession(javax.net.ssl.SSLSession) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) SSLContext(javax.net.ssl.SSLContext) HttpClientConnectionManager(org.apache.hc.client5.http.io.HttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate)

Aggregations

SSLConnectionSocketFactory (org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory)7 HttpClientConnectionManager (org.apache.hc.client5.http.io.HttpClientConnectionManager)5 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)4 PoolingHttpClientConnectionManager (org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)4 ConnectionSocketFactory (org.apache.hc.client5.http.socket.ConnectionSocketFactory)4 PlainConnectionSocketFactory (org.apache.hc.client5.http.socket.PlainConnectionSocketFactory)4 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)3 IOException (java.io.IOException)2 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)2 SSLContextBuilder (org.apache.hc.core5.ssl.SSLContextBuilder)2 Client (feign.Client)1 ApacheHttp5Client (feign.hc5.ApacheHttp5Client)1 ApacheHttpClient (feign.httpclient.ApacheHttpClient)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 X509Certificate (java.security.cert.X509Certificate)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutorService (java.util.concurrent.ExecutorService)1 SSLContext (javax.net.ssl.SSLContext)1