1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import javax.net.ssl.HttpsURLConnection; import java.net.URL; import java.security.cert.Certificate; import java.security.cert.X509Certificate;
public class Main {
public static void main(String[] args) throws Exception { URL url = new URL("https://www.baidu.com"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.connect(); for (Certificate certificate : connection.getServerCertificates()) { //第一个就是服务器本身证书,后续的是证书链上的其他证书 X509Certificate x509Certificate = (X509Certificate) certificate; System.out.println(x509Certificate.getSubjectDN()); System.out.println(x509Certificate.getNotBefore());//有效期开始时间 System.out.println(x509Certificate.getNotAfter());//有效期结束时间 } connection.disconnect(); } }
|