以编程方式从Url Android获取证书信息

时间:2015-04-28 08:47:31

标签: android ios ssl-certificate x509certificate

是否可以从Url获取证书信息?在iOS中,它有NSURLAuthenticationChallenge,如果url包含https,则会提供相关信息。

我们以同样的方式通过代码获取特定url的证书信息吗?

2 个答案:

答案 0 :(得分:2)

X509TrustManager trustManager = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                    for (TrustManager tm : managers) {
                        if (tm instanceof X509TrustManager) {
                            ((X509TrustManager) tm).checkClientTrusted(
                                    chain, authType);
                        }
                    }
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                    for (X509Certificate cert : chain) {
                        // cert gives the server Certificate Information.
                        if (cert.getIssuerX500Principal().equals(
                                trustedRoot.getIssuerX500Principal())) {
                            return;
                        }
                    }
                    for (TrustManager tm : managers) {
                        if (tm instanceof X509TrustManager) {
                            ((X509TrustManager) tm).checkServerTrusted(
                                    chain, authType);
                        }
                    }
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    ArrayList<X509Certificate> issuers = new ArrayList<>();
                    for (TrustManager tm : managers) {
                        if (tm instanceof X509TrustManager) {
                            issuers.addAll(Arrays
                                    .asList(((X509TrustManager) tm)
                                            .getAcceptedIssuers()));
                        }
                    }
                    return issuers.toArray(new X509Certificate[issuers
                            .size()]);
                }

            };

在上面的代码中检查此// cert gives the server Certificate Information.

答案 1 :(得分:0)

是的,有可能:

步骤1:在broswer中输入网址,点击2标签,如下所示:

enter image description here

第2步:打开详情选项卡

第3步:从那里导出公钥。

enter image description here

第4步:获取.cert文件。

步骤5:从.cert文件生成密钥库并将其放在android