dreadnightmare
dreadnightmare

Reputation: 47

Deprecated HttpClient, now using apache-httpclient-4.3.x

I have code that works fine but its now deprecated. Everything is depricated except BasicCredentialsProvider

SSLSocketFactory sslsf = new SSLSocketFactory("TLS", null, null, keyStore, null, new TrustSelfSignedStrategy(),
                new AllowAllHostnameVerifier());
        Scheme https = new Scheme("https", 28181, sslsf);

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(https);

        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
        connectionManager.setMaxTotal(100);
        connectionManager.setDefaultMaxPerRoute(5);

        DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);

        BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort(), name),
                new UsernamePasswordCredentials(username, password));

        httpClient.setCredentialsProvider(credsProvider);
        httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

        return new HttpContextRequestScopedApacheClientExecutor(httpClient);

I have tried to do it myself. First I replaced SSLSocketFactory with

    SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory
(SSLContexts.custom().
                    loadTrustMaterial(keyStore, new TrustSelfSignedStrategy()).
useTLS().build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

then I tried to use

Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslConnectionFactory) .build();

I couldn't fit my port there, so I after searching for quite some time I am still lost on how to do it. And for the rest I have no solution yet. Any help on this would be apriciated.

Upvotes: 2

Views: 2532

Answers (1)

dreadnightmare
dreadnightmare

Reputation: 47

I think I have managed to do it, it's working so far.

HttpClientBuilder builder = HttpClientBuilder.create();
    KeyStore keyStore = initSSL();

    SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(keyStore, new
    TrustSelfSignedStrategy()).useTLS().build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() 
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslConnectionFactory)
            .build();

    PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(registry);
    ccm.setMaxTotal(100);
    ccm.setDefaultMaxPerRoute(5);
    builder.setConnectionManager(ccm);

    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort(), name),
            new UsernamePasswordCredentials(username, password));

    builder.setDefaultCredentialsProvider(credsProvider);   

    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder.setSocketTimeout(60000); 
    builder.setDefaultRequestConfig(requestBuilder.build());

    return new HttpContextRequestScopedApacheClientExecutor(builder.build());

Upvotes: 1

Related Questions