配置HTTPS证书

private HttpsURLConnection httpsConfig() throws Exception{
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream in = activity.getAssets().open("new-jzbank.cer");//new-jzbank.cer为证书文件
Certificate cartificate = cf.generateCertificate(in);
in.close();
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(null, null);
keystore.setCertificateEntry("trust", cartificate);

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keystore);

SSLContext context = SSLContext.getInstance("TLS");
context.init(null, trustManagerFactory.getTrustManagers(), null);

HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());

return urlConnection;
}
posted @ 2017-09-22 09:41  四郎青初  阅读(460)  评论(0编辑  收藏  举报