Ignore self-signed certificates in Apache HTTPClient 4.5

后端 未结 4 848
深忆病人
深忆病人 2021-01-02 02:44

I am trying to accept all certificates, and/or accept self-signed certificates using Apache HTTPClient version 4.5 (tutorial link here)

4条回答
  •  青春惊慌失措
    2021-01-02 03:10

    I'm using Apache HttpClient 4.5.3 and none of the above solutions helped. I always got the error

    PKIX Path building failed

    .

    I found the solution in http://www.baeldung.com/httpclient-ssl

    Here's my code:

    try {
        SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, (certificate, authType) -> true).build();
        httpClient = HttpClients.custom().setSSLContext(sslContext)
                .setSSLHostnameVerifier(new NoopHostnameVerifier())
                .build();
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题