使用useSystemProperties()创建HttpClient

时间:2019-03-27 19:34:41

标签: java apache http httpclient

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

我尝试了3种方法,

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


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


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

我真的可以同时实现这两种选择吗?

1 个答案:

答案 0 :(得分:0)

您需要为SSLConnectionSocketFactory覆盖ConnectionManager,例如,如果您使用SSLConnectionSocketFactory useSystemProperties >

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