If I'm right you have to create a certificate, sign it and include it in your app. Or change the server configuration (further information here).
Otherwise you can trust every handshake within your app. This is not the best approach, but really useful during implementation.
Include this class in your project
public class SSLCertificateHandler {
protected static final String TAG = "NukeSSLCerts";
/**
* Enables https connections
*/
public static void nuke() {
try {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
X509Certificate[] myTrustedAnchors = new X509Certificate[0];
return myTrustedAnchors;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
} catch (Exception e) {
}
}
}
Extend your Application
, and call the 'nuke' function in your onCreate
public class YOURApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//...
// trust all SSL -> HTTPS connection
SSLCertificateHandler.nuke();
}
I found this code in SO, but can't find the link at the moment....
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…