首页 > Web开发 > 详细

httpclient5:信任所有证书,调用公众号接口

时间:2021-05-13 01:02:33      阅读:23      评论:0      收藏:0      [点我收藏+]
// Trust standard CA and those trusted by our custom strategy
    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
      // 信任所有
      public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        return true;
      }
    }).build();
    // Allow TLSv1.2 protocol only
    final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
        .setSslContext(sslContext).setTlsVersions(TLS.V_1_2).build();
    final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
        .setSSLSocketFactory(sslSocketFactory).build();
    try (CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build()) {
    // 获得access_token
      final HttpGet httpget = new HttpGet(
          "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=自己的&secret=自己的");
      System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
      final HttpClientContext clientContext = HttpClientContext.create();
      try (CloseableHttpResponse response = httpclient.execute(httpget, clientContext)) {
        System.out.println("----------------------------------------");
        System.out.println(response.getCode() + " " + response.getReasonPhrase());
        System.out.println(EntityUtils.toString(response.getEntity()));

        final SSLSession sslSession = clientContext.getSSLSession();
        if (sslSession != null) {
          System.out.println("SSL protocol " + sslSession.getProtocol());
          System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
        }
      }
    }

httpclient5:信任所有证书,调用公众号接口

原文:https://www.cnblogs.com/huiy/p/14761639.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!