如何获取IP地址URL的SSl证书到期日期

时间:2017-04-13 10:17:35

标签: java ssl

我正在接受一项任务,即获取域名网站的ssl到期日期以及通过IP访问的日期。我写了下面的代码。当我给出域名时,它给了我正确的输出,但当我在URl中给出IP地址,即192.168.1.1时,它会抛出javax.net.ssl.SSLException:收到致命警报:protocol_version错误。你能让我知道我在这里做错了吗

public static void main(String[] args) throws Exception {
    URL url = new URL("https://google.co.in");
    System.out.println("Expiration Date: "+getCertificateExpiration(url));
}

/**
 * Returns the expiration (notAfter) date of the X.509 Certificate
 * used to encrypt the HTTPS connection of the given URL.  If the
 * connection is not a secure HTTPS connection or the server is not
 * signed with an X.509 certificate, this method returns null.  You
 * may want to modify the implementation to throw relevant
 * exceptions if you need to handle those conditions separately.
 * @param url the URL to connect to
 * @return the expiration (notAfter) date of the server's X.509 Certificate or
 * null if unable to connect, the connection is not secure, or the server is
 * not signed with an X.509 certificate.
 */
public static Date getCertificateExpiration(URL url) {
    try {
        URLConnection conn = url.openConnection();
        conn.connect();
        if (conn instanceof HttpsURLConnection) {
            /*retrieve the N-length signing chain for the server certificates.
            certs[0] is the server's certificate.
            certs[1] - certs[N-1] are the intermediate authorities that signed the cert.
            certs[N] is the root certificate authority of the chain. */
            Certificate[] certs = ((HttpsURLConnection)conn).getServerCertificates();
            if (certs.length > 0 && certs[0] instanceof X509Certificate) {
                // certs[0] is an X.509 certificate, return its "notAfter" date
                return ((X509Certificate)certs[0]).getNotAfter();
            }
        }
        // connection is not HTTPS or server is not signed with an X.509 certificate, return null
        return null;
    } catch (SSLPeerUnverifiedException spue) {
        // connection to server is not verified, unable to get certificates
        return null;
    } catch (IllegalStateException ise) {
        // shouldn't get here -- indicates attempt to get certificates before
        // connection is established
        return null;
    } catch (IOException ioe) {
        // error connecting to URL -- this must be caught last since
        // other exceptions are subclasses of IOException
        return null;
    }
}

错误Stacktrace如下

javax.net.ssl.SSLException: Received fatal alert: protocol_version
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
at com.test.SSLTest.getCertificateExpiration(SSLTest.java:39)
at com.test.SSLTest.main(SSLTest.java:21)

0 个答案:

没有答案