在调用第三方接口的时候出现
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid
http请求忽略ssl证书
在调用第三方的接口的时候,通过浏览器直接访问接口地址是能访问,通过postman测试是不行的,通过接口调用也是不行的,最后发现域名是https的,请求的时候要忽略ssl证书
引入的jar包如下
import javax.net.ssl.SSLContext; import java.security.cert.CertificateException; import java.security.cert.X509Certificate;
public static CloseableHttpClient getIgnoeSSLClient() { SSLContext sslContext=null; try{ sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }).build(); }catch (Exception e){ e.printStackTrace(); } //创建httpClient CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext). setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); return client; }