无法使用翻新2添加SSL证书

时间:2018-10-02 16:39:29

标签: java android ssl retrofit

无法使用Retrofit 2添加SSL证书,但出现以下错误:

java.net.UnknownHostException:无法解析主机“ abcd.com”:没有与主机名关联的地址

下面是代码:

     try {
cf = CertificateFactory.getInstance("X.509");
cert = context.getResources().openRawResource(R.raw.abcd); 
ca = cf.generateCertificate(cert);
cert.close();

String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);

client = new OkHttpClient.Builder()
.sslSocketFactory(sslContext.getSocketFactory())
.build();

} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException | KeyManagementException e) {
Log.e("Certificate Exception",e.toString());
e.printStackTrace();
}
catch (Exception e)
{
Log.e("Certificate Exception",e.toString());
e.printStackTrace();
}

0 个答案:

没有答案