Open top menu



How to solve SSL.SSLHandshakeException in Android?

Basically ,SSL usage Scenario, a server configurd  with a certificate containing a public key as well as a Matching private key.As part of handshake between an SLL client and server.

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) {
      }
  } };
 
final SSLContext sc = SSLContext.getInstance("SSL");
  sc.init(
null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 
// Create all-trusting host name verifier
 
HostnameVerifier allHostsValid = new HostnameVerifier() {
     
public boolean verify(String hostname, SSLSession session) {
         
return true;
      }
  };
 
// Install the all-trusting host verifier
 
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  URL url =
new URL(aurl[0]);
//  HttpsURLConnection connection = (HttpsURLConnection) (url.openConnection());
 
URLConnection connection = url.openConnection();
  connection.setRequestProperty(
"connection", "close");
  connection.connect();
  


Try this, 
Tagged

0 comments