Android examples for Network:HTTPS SSL
new Ssl Socket Factory
import android.content.Context; import android.support.annotation.RawRes; import java.io.InputStream; import java.security.KeyStore; import java.security.SecureRandom; import java.security.cert.Certificate; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.security.cert.CertificateException; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; public class Main{ /*from www. j ava 2 s .com*/ public static SSLSocketFactory newSslSocketFactory(Context context, @RawRes int id) { try { // Get an instance of the Bouncy Castle KeyStore format //KeyStore trusted = KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore trusted = KeyStore.getInstance("PKCS12", "BC"); InputStream in = context.getResources().openRawResource(id); CertificateFactory cerFactory = CertificateFactory .getInstance("X.509"); Certificate cer; try { cer = cerFactory.generateCertificate(in); } finally { in.close(); } trusted.load(null, null); trusted.setCertificateEntry("ca", cer); String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory .getInstance(tmfAlgorithm); tmf.init(trusted); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tmf.getTrustManagers(), null); return sslContext.getSocketFactory(); } catch (Exception e) { throw new AssertionError(e); } } }