相关疑难解决方法(0)

SSLHandshakeException未找到Android HTTPS的证书路径的信任锚

我正在尝试建立与HTTPS站点的连接,我得到了这个例外:javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
我原来的代码如下:

URL url = new URL("https://example.com");
            HttpsURLConnection urlConnection =
                (HttpsURLConnection)url.openConnection();
            in = urlConnection.getInputStream();
            byte[] responsedata = CommonUtil.readInputStream(in);
            Log.w(TAG, "response is "+CommonUtil.convertBytesToHexString(responsedata));
Run Code Online (Sandbox Code Playgroud)

然后,我在这里阅读Google的文章,并将我的代码修改为:

CertificateFactory cf;
        try {
            cf = CertificateFactory.getInstance("X.509");
            InputStream in = this.mContext.getResources().openRawResource(R.raw.cert);
            Certificate ca;
            ca = cf.generateCertificate(in);
                System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
                in.close();
                URL url = new URL("https://example.com");
                HttpsURLConnection urlConnection =
                (HttpsURLConnection)url.openConnection();
                in = urlConnection.getInputStream();
                byte[] responsedata = CommonUtil.readInputStream(in);
            Log.w(TAG, "response is "+CommonUtil.convertBytesToHexString(responsedata));
            in.close();  
Run Code Online (Sandbox Code Playgroud)

关于 …

java ssl https android openssl

11
推荐指数
1
解决办法
3万
查看次数

标签 统计

android ×1

https ×1

java ×1

openssl ×1

ssl ×1