package com.marshalchen.common.commonUtils.urlUtils; import android.content.Context; import com.marshalchen.common.commonUtils.logUtils.Logs; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; import java.io.*; import java.net.URL; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; public class HttpsUtils { public static Certificate getX509CertifaceteFromCrtFile() { InputStream inStream = null; X509Certificate cert = null; try { inStream = new FileInputStream("fileName-of-cert"); CertificateFactory cf = CertificateFactory.getInstance("X.509"); cert = (X509Certificate) cf.generateCertificate(inStream); inStream.close(); } catch (Exception e) { e.printStackTrace(); Logs.e(e, ""); } finally { return cert; } } public static SSLSocketFactory buildSslSocketFactory(Context context,String crtUrl) { try { // Load CAs from an InputStream // (could be from a resource or ByteArrayInputStream or ...) CertificateFactory cf = CertificateFactory.getInstance("X.509"); // From https://www.washington.edu/itconnect/security/ca/load-der.crt InputStream is = context.getResources().getAssets().open(crtUrl); InputStream caInput = new BufferedInputStream(is); Certificate ca; try { ca = cf.generateCertificate(caInput); // System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN()); } finally { caInput.close(); } // Create a KeyStore containing our trusted CAs String keyStoreType = KeyStore.getDefaultType(); KeyStore keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(null, null); keyStore.setCertificateEntry("ca", ca); // Create a TrustManager that trusts the CAs in our KeyStore String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore); // Create an SSLContext that uses our TrustManager SSLContext contexts = SSLContext.getInstance("TLS"); contexts.init(null, tmf.getTrustManagers(), null); return contexts.getSocketFactory(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * @deprecated */ public static void sendWithSSlSocket() { SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); URL url = null; try { url = new URL("https://xxx.xxx"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(sslsocketfactory); InputStream inputstream = conn.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); String string = null; while ((string = bufferedreader.readLine()) != null) { System.out.println("Received " + string); } } catch (Exception e) { e.printStackTrace(); Logs.e(e, ""); } } public static void sendWithSSlSocketWithCrt(Context context, String crtUri,String uri) { SSLSocketFactory sslsocketfactory = buildSslSocketFactory(context,crtUri); URL url = null; try { url = new URL(uri); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(sslsocketfactory); InputStream inputstream = conn.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); String string = null; while ((string = bufferedreader.readLine()) != null) { Logs.d("Received " + string); } } catch (Exception e) { e.printStackTrace(); Logs.e(e, ""); } } }