How to get the Certificate Information in Java

浪子不回头ぞ 提交于 2020-01-21 08:42:26

问题


I am trying to get and read a certificate only in Java. What code or examples should I look at to get the certificates of a website.

For examples the websites:

  1. https://google.com
  2. https://www.ssllabs.com/

Do I use the URL class?

URL url = new URL("https://google.com");

回答1:


Ok I just found out how I can get the information that I want.

public void certInformation(String aURL) throws Exception{
        URL destinationURL = new URL(aURL);
        HttpsURLConnection conn = (HttpsURLConnection) destinationURL.openConnection();
        conn.connect();
        Certificate[] certs = conn.getServerCertificates();
        for (Certificate cert : certs) {
            System.out.println("Certificate is: " + cert);
            if(cert instanceof X509Certificate) {
                    X509Certificate x = (X509Certificate ) cert;
                    System.out.println(x.getIssuerDN());
            }
        }
    }


来源:https://stackoverflow.com/questions/22464955/how-to-get-the-certificate-information-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!