Last active
August 21, 2023 11:47
-
-
Save mingliangguo/c86e05a0f8a9019b281a63d151965ac7 to your computer and use it in GitHub Desktop.
disable SSL and host name verification for apache httpclient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
TrustManager[] trustAllCerts = new TrustManager[] { | |
new X509TrustManager() { | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
public void checkClientTrusted(X509Certificate[] certs, String authType) { } | |
public void checkServerTrusted(X509Certificate[] certs, String authType) { } | |
} | |
}; | |
SSLContext sc = SSLContext.getInstance("SSL"); | |
sc.init(null, trustAllCerts, new SecureRandom()); | |
CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSSLContext(sc).build(); | |
String output = Executor.newInstance(httpClient).execute(Request.Get("https://127.0.0.1:3000/something") | |
.connectTimeout(1000) | |
.socketTimeout(1000)).returnContent().asString(); | |
} catch (Exception e) { | |
} | |
# source: http://stackoverflow.com/questions/2703161/how-to-ignore-ssl-certificate-errors-in-apache-httpclient-4-0 | |
// update on March 2022, the following is a much simpler version to disable the SSL check with Apache httpclient | |
var httpClient = HttpClients | |
.custom() | |
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build()) | |
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) | |
.build(); |
This helps a lot, Thanks
Thanks, you saved my day
I love you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From documentation
setSslcontext(SSLContext sslcontext)
Deprecated. (4.5) use setSSLContext(SSLContext)