Created
October 19, 2015 20:37
-
-
Save budhash/4c21e87fb1801a8c19c0 to your computer and use it in GitHub Desktop.
SSLTrustAll - Apache HTTP Client
This file contains hidden or 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
| // Create a trust manager that does not validate certificate chains | |
| TrustManager[] trustAllCerts = new TrustManager[] { | |
| new X509TrustManager() { | |
| public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
| return null; | |
| } | |
| public void checkClientTrusted( | |
| java.security.cert.X509Certificate[] certs, String authType) { | |
| } | |
| public void checkServerTrusted( | |
| java.security.cert.X509Certificate[] certs, String authType) { | |
| } | |
| } | |
| }; | |
| // Install the all-trusting trust manager | |
| try { | |
| SSLContext sc = SSLContext.getInstance("SSL"); | |
| sc.init(null, trustAllCerts, new java.security.SecureRandom()); | |
| HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); | |
| } catch (GeneralSecurityException e) { | |
| } | |
| SSLContext sslContext = SSLContext.getInstance("SSL"); | |
| // set up a TrustManager that trusts everything | |
| sslContext.init(null, new TrustManager[] { new X509TrustManager() { | |
| public X509Certificate[] getAcceptedIssuers() { | |
| System.out.println("getAcceptedIssuers ============="); | |
| return null; | |
| } | |
| public void checkClientTrusted(X509Certificate[] certs, | |
| String authType) { | |
| System.out.println("checkClientTrusted ============="); | |
| } | |
| public void checkServerTrusted(X509Certificate[] certs, | |
| String authType) { | |
| System.out.println("checkServerTrusted ============="); | |
| } | |
| } }, new SecureRandom()); | |
| SSLSocketFactory sf = new SSLSocketFactory(sslContext); | |
| Scheme httpsScheme = new Scheme("https", 443, sf); | |
| SchemeRegistry schemeRegistry = new SchemeRegistry(); | |
| schemeRegistry.register(httpsScheme); | |
| // apache HttpClient version >4.2 should use BasicClientConnectionManager | |
| ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry); | |
| HttpClient httpClient = new DefaultHttpClient(cm); | |
| System.setProperty ("jsse.enableSNIExtension", "false"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment