忽略证书验证的CloseableHttpClient

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010430495/article/details/72967591

项目里需要忽略证书访问,在网上查了,大部分回答都是复制粘贴的,并且也没有什么结果。

自己看看源码,现在把创建忽略证书的CloseableHttpClient的方法共享下


public CloseableHttpClient getIgnoeSSLClient() throws Exception {
   SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
      @Override
      public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
         return true;
      }
   }).build();

   //创建httpClient
   CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).
         setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
   return client;
}

此方法返回的client可以忽略证书验证


猜你喜欢

转载自blog.csdn.net/u010430495/article/details/72967591