Unable to find valid certification path to requested target - java

前端 未结 2 761
独厮守ぢ
独厮守ぢ 2021-02-05 16:56

I\'m trying to connect to a website using a HttpClient object. It works fine for websites we normally use(Like google). But there is a web site, when I try to connect, my progra

相关标签:
2条回答
  • 2021-02-05 17:33

    For HttpClient4.x, the following will trust all

    public static HttpClientBuilder createTrustAllHttpClientBuilder() {
      SSLContextBuilder builder = new SSLContextBuilder();
      builder.loadTrustMaterial(null, (chain, authType) -> true);           
      SSLConnectionSocketFactory sslsf = new 
      SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
      return HttpClients.custom().setSSLSocketFactory(sslsf);
    }
    
    0 讨论(0)
  • 2021-02-05 17:43

    Problem was solved when I used a TrustSelfSignedStrategy object as the Trust material to HttpClient.

            httpClient = HttpClients.custom()
                .setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom()
                        .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                        .build()
                    )
                ).build();
    

    The code I used is shown above..

    0 讨论(0)
提交回复
热议问题