在验证HTTPS URL时遇到问题

时间:2018-01-05 18:36:58

标签: java url httpurlconnection

当我使用下面的代码点击huc.connect行以验证URL存在时,我收到下面的堆栈跟踪错误。我可以使用与HTTP相同的链接而不是HTTP,它工作正常。我也试过使用HttpsURLConnection但在堆栈跟踪中有相同的错误。我可以使用另一种方法来验证URL是否存在?

URL u = new URL("https://www.postgresql.org/docs/9.4/static/catalogs.html"); 
//u.toURI();  //to check the syntax
HttpURLConnection huc =  (HttpURLConnection)u.openConnection();
huc.setRequestMethod("GET");
//huc.setRequestMethod("HEAD");
huc.connect(); 
System.out.println(huc.getResponseCode());

Strack Trace:

Exception in thread "main" javax.net.ssl.SSLException: Received fatal alert: protocol_version
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
    at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1959)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1077)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at UrlTester2.main(UrlTester2.java:31)

1 个答案:

答案 0 :(得分:0)

您可以使用Apache HttpClient来验证URL,但是如果您升级到最新版本的Java 1.8,您的代码应该可以使用。