CloseableHttpAsyncClient忽略https的证书验证

https比http安全是因为,网络通信协议里边有数字签名和证书等概念

阮一峰写的科普文解释数字签名:http://www.ruanyifeng.com/blog/2011/08/what_is_a_digital_signature.html


使用apache httpclient客户端api忽略证书验证代码:

初始化连接池客户端:

private static PoolingNHttpClientConnectionManager pool;

    private static CloseableHttpAsyncClient httpAsyncClient = null;

    static {
        try {
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
                            // don't check
                        }
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
                            // don't check
                        }
                    }
            };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, null);
            SSLIOSessionStrategy sslioSessionStrategy = new SSLIOSessionStrategy(sslContext, SSLIOSessionStrategy.ALLOW_ALL_HOSTNAME_VERIFIER);
            Registry<SchemeIOSessionStrategy> registry = RegistryBuilder.<SchemeIOSessionStrategy>create()
                    .register("http", NoopIOSessionStrategy.INSTANCE)
                    .register("https", sslioSessionStrategy)
                    .build();
            ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
            pool = new PoolingNHttpClientConnectionManager(ioReactor,registry);
            pool.setMaxTotal(100); // 设置最多连接数
            pool.setDefaultMaxPerRoute(20); // 设置每个host最多20个连接数
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000) // 设置请求响应超时时间
                    .setConnectTimeout(12000) // 设置请求连接超时时间
                    .build();

            httpAsyncClient = HttpAsyncClients.custom().setConnectionManager(pool) // 设置连接池
                    .setDefaultRequestConfig(requestConfig) // 设置请求配置
                    .build();
            httpAsyncClient.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

调用post

HttpPost post = new HttpPost(url); // 创建POSt请求
            // 设置参数到请求对象中
            post.setEntity(new StringEntity(parames, encoding));
            if (StringUtil.isNotEmpty(host)) {
                post.setHeader("Host", host);
            }
            // 执行请求操作,并拿到结果(异步)
            Future<HttpResponse> resultFuture = httpAsyncClient.execute(post, null);// 设置请求参数
            HttpResponse response = resultFuture.get(timeout, TimeUnit.MILLISECONDS);
            LogUtil.info("asyn post cost",url,parames,System.currentTimeMillis() - start);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null)
                result = EntityUtils.toString(httpEntity, encoding);

https://hc.apache.org/httpcomponents-asyncclient-dev/quickstart.html


其中execute使用FutureCallback<HttpResponse> callback参数和不使用区别没搞懂,感觉异步方法都是reactor模型实现的异步阻塞。在等待返回结果


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值