2021年8月9日 | Leave a comment 问题: Caused by: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:350) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:293) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:288) 12345 Caused by: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:350) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:293) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:288) 代码 Java @Test void dd() throws IOException, InterruptedException { var c = HttpClient.newHttpClient(); var req = HttpRequest.newBuilder(URI.create("https://www.sxpdf.com/lishi/page/3/")).GET().build(); final var resp = c.send(req, HttpResponse.BodyHandlers.ofString()).body(); assertNotNull(resp); } 1234567 @Test void dd() throws IOException, InterruptedException { var c = HttpClient.newHttpClient(); var req = HttpRequest.newBuilder(URI.create("https://www.sxpdf.com/lishi/page/3/")).GET().build(); final var resp = c.send(req, HttpResponse.BodyHandlers.ofString()).body(); assertNotNull(resp); } 解决: .sslContext(sslContext()) @Test void dd2() throws IOException, InterruptedException { var c = HttpClient.newBuilder() .sslContext(sslContext()) .build(); var req = HttpRequest.newBuilder(URI.create("https://www.sxpdf.com/lishi/page/3/")).GET().build(); final var resp = c.send(req, HttpResponse.BodyHandlers.ofString()).body(); assertNotNull(resp); } private javax.net.ssl.SSLContext sslContext() { try { //org.apache.http.ssl.SSLContextBuilder#SSLContextBuilder return new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { return true; } }).build(); } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) { throw new RuntimeException(e); } } 1234567891011121314151617181920212223 @Test void dd2() throws IOException, InterruptedException { var c = HttpClient.newBuilder() .sslContext(sslContext()) .build(); var req = HttpRequest.newBuilder(URI.create("https://www.sxpdf.com/lishi/page/3/")).GET().build(); final var resp = c.send(req, HttpResponse.BodyHandlers.ofString()).body(); assertNotNull(resp); } private javax.net.ssl.SSLContext sslContext() { try {//org.apache.http.ssl.SSLContextBuilder#SSLContextBuilder return new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { return true; } }).build(); } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) { throw new RuntimeException(e); } } 参考: https://blog.csdn.net/qq_42561919/article/details/100065675