![](/img/trans.png)
[英]Will client JVM for a web service(https) throw an SSL Exception when the server is having a valid certificate from a CA?
[英]How to download a binary large file throw HTTPS when the web server require a client TLS certificate?
我沒有找到任何使用 DownloadManager 實現 SSLContext 的方法。 有沒有辦法添加客戶端證書(密鑰庫)?
目前,它是一個自簽名證書(客戶端和服務器)。 我可以使用 okhttp(管理 SSLContext)連接到該服務器,但是使用 DownloadManager 我收到錯誤“SSL 握手”。
這是我的代碼,
@Nullable
private static SSLContext initTrustManager(Context context) {
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
InputStream is = context.getAssets().open("s_cert.cer");
Certificate ca;
try {
ca = certificateFactory.generateCertificate(is);
Log.i("TrustManager", "ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
is.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 = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
return sslContext;
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return null;
}
這是我實現它的方法:
builder.sslSocketFactory(initTrustManager(context).getSocketFactory());
這是工作代碼,所以如果仍然出現異常,請注意 SSL 證書本身或在服務器的 api 內部進行一些更改。 希望能幫助到你))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.