b2byco
2022-12-13 22:48:32 +08:00
试试这个
try {
X509TrustManager x509TrustManager = 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("TLSv1.2");
sslContext.init((KeyManager[]) null, new X509TrustManager[] {
x509TrustManager
}, (SecureRandom) null);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).followRedirects(HttpClient.Redirect.ALWAYS).version(HttpClient.Version.HTTP_1_1).build();
URI uri = new URI("https", "//", null);
HttpRequest r = HttpRequest.newBuilder().uri(uri).GET().build();
var s = client.send(r, HttpResponse.BodyHandlers.ofString());
System.out.println(s.body());
} catch(Exception e) {}