【问题标题】:create HttpClient using useSystemProperties()使用 useSystemProperties() 创建 HttpClient
【发布时间】:2019-03-27 19:34:41
【问题描述】:

我正在尝试使用 useSystemProperties() 创建一个 http 客户端,因为我需要将 ssl 属性默认为 WAS 的属性[比如在运行时获取 WAS 密码]。而且我还必须为 httpclient 设置一些最大连接数和连接管理器。这是一个非常高流量的休息电话。

我尝试了 3 种方法,

 -- This did not set the WAS ssl properties and thus the connection got failed.
httpclient = HttpClients.custom().useSystemProperties()
                        .setConnectionManager("some value")
                        .setMaxConnPerRoute("some value")
                        .setMaxConnTotal("some value")
                        .setUserAgent("Custom Browser")
                        .disableCookieManagement().build();

 -- This did not set the WAS ssl properties and thus the connection failed.
httpclient1 = HttpClientBuilder.create().useSystemProperties()
                               .setConnectionManager(connManager)
                               .setMaxConnPerRoute(maxConnPerRoute)
                               .setMaxConnTotal(maxConnTotal)
                               .setUserAgent("Custom Browser")
                               .disableCookieManagement().build();

-- This one defaulted to WAS ssl configurations and connection was fine but other params are missing here.
httpclient2 = HttpClientBuilder.create().useSystemProperties().build();

我真的可以同时实现这两个选项吗?

【问题讨论】:

    标签: java apache http httpclient


    【解决方案1】:

    您需要为您的ConnectionManager 覆盖SSLConnectionSocketFactory,例如,如果您使用useSystemProperties,下面的示例将创建默认SSLConnectionSocketFactory

        DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(PublicSuffixMatcherLoader.getDefault());
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                (SSLSocketFactory) SSLSocketFactory.getDefault(), null, null, hostnameVerifier
        );
    
        final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", PlainConnectionSocketFactory.getSocketFactory())
                        .register("https", sslConnectionSocketFactory)
                        .build()
        );
    
        connManager.setDefaultMaxPerRoute(20);
        connManager.setMaxTotal(20);
    
    
        final HttpClientBuilder builder = HttpClientBuilder
                .create()
                .setConnectionManager(connManager);
    

    【讨论】:

      【解决方案2】:

      您也可以将useSystemProperties() 设置为您的HttpClient

      HttpClient httpClient = HttpClientBuilder.create()
                  .setConnectionManager(connectionManager)
                  .useSystemProperties()
                  .setDefaultRequestConfig(requestConfig).build();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多