不推荐使用HttpClient,现在使用apache-httpclient-4.3.x

时间:2016-07-29 08:40:05

标签: java apache-httpclient-4.x

我的代码工作正常,但现在已弃用。除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);

我自己试过这样做。首先,我用

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

然后我尝试使用

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

我无法在那里安装我的港口,所以在搜索了很长一段时间后,我仍然迷失在如何做到这一点。其余的我还没有解决方案。任何有关这方面的帮助都会受到影响。

1 个答案:

答案 0 :(得分:1)

我想我已经设法做到了,它到目前为止一直在努力。

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());