你好,我需要与https://free.temafon.ru建立https连接,但在Android 2.3及以下版本中出现了CertPathValidatorException错误。我该怎么办?
- Grab all certs from https://free.temafon.ru with Firefox.
- Import certs in keystore in sequence from temefon certificate to root certificate.
Init ssl context:
final KeyStore keystore = KeyStore.getInstance("BKS");
keystore.load(getResources().openRawResource(R.raw.temafon), "W0d3Uoa5PkED".toCharArray()); final TrustManager trustManager = new TemafonTrustManager(keystore); final SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { trustManager }, null); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext .getSocketFactory());
Here, I use custom TrustManager, because server sends certs in wrong order.
这段代码在Android 4.0上可以正常工作,但在2.3上失败了,提示错误信息为java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
我做错了什么?
我创建了一个测试项目,可以在这里找到。