1.关闭SSL验证
private static SSLContext createUnsafeSSLContext() throws NoSuchAlgorithmException, KeyManagementException {
var trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
return sslContext;
}
2. 取消主机名验证
System.setProperty("jdk.internal.httpclient.disableHostnameVerification", "true");
3. 构建 HttpClient 实例
try (HttpClient client = HttpClient.newBuilder().sslContext(unsafeSSLContext).build()) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.GET()
.build();
...
}