×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Massimo Zappino
Added: Feb 24, 2014 8:13 PM
Modified: Dec 28, 2015 11:33 AM
Views: 10
  1.     static {
  2.         disableSslVerification();
  3.     }
  4.  
  5.     private static void disableSslVerification() {
  6.         try {
  7.             // Create a trust manager that does not validate certificate chains
  8.             TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
  9.                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  10.                     return null;
  11.                 }
  12.  
  13.                 public void checkClientTrusted(X509Certificate[] certs, String authType) {
  14.                 }
  15.  
  16.                 public void checkServerTrusted(X509Certificate[] certs, String authType) {
  17.                 }
  18.             }
  19.             };
  20.  
  21.             // Install the all-trusting trust manager
  22.             SSLContext sc = SSLContext.getInstance("SSL");
  23.             sc.init(null, trustAllCerts, new java.security.SecureRandom());
  24.             HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  25.  
  26.             // Create all-trusting host name verifier
  27.             HostnameVerifier allHostsValid = new HostnameVerifier() {
  28.                 public boolean verify(String hostname, SSLSession session) {
  29.                     return true;
  30.                 }
  31.             };
  32.  
  33.             // Install the all-trusting host verifier
  34.             HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  35.         } catch (NoSuchAlgorithmException e) {
  36.             e.printStackTrace();
  37.         } catch (KeyManagementException e) {
  38.             e.printStackTrace();
  39.         }
  40.     }