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

Get Certificate Information from Url Android Programmatically

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

同样的方法,我们有没有办法通过代码获取特定url的证书信息?

是的,这是可能的:

Step1 : 在broswer中输入url,点击2标签如下图:

第 2 步:打开详细信息选项卡

第 3 步:从那里导出 public 密钥。

第 4 步:获取 .cert 文件。

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

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.