javax.net.ssl.SSLHandshakeException 而来自 java 代码的 HttpsURLConnection

标签 java http https connection

我有一个简单的java代码,它只打开一个HttpsURLConnection,它会产生SSLHandshakeException,但链接https://www.zlti.com在浏览器中打开且仅通过代码时访问正常,面临 SSLHandshakeException。

public static void main(String[] args) {

    String url = "https://www.zlti.com";
    HttpsURLConnection huc = null;        
    try {
    huc = (HttpsURLConnection)(new URL(url).openConnection());
    huc.getResponseCode();
    } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

完整的堆栈跟踪为

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1959)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1514)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:961)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1564)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
at Automate.BrokenLinks.main(BrokenLinks.java:29)

最佳答案

添加以下代码后即可工作

try {
    SSLContext ssl_ctx = SSLContext.getInstance("TLS");
        TrustManager[ ] trust_mgr = new TrustManager[ ] {
    new X509TrustManager() {
       public X509Certificate[ ] getAcceptedIssuers() { return null; }
       public void checkClientTrusted(X509Certificate[ ] certs, String t) { }
       public void checkServerTrusted(X509Certificate[ ] certs, String t) { }
     }
  };
        ssl_ctx.init(null,                // key manager
                     trust_mgr,           // trust manager
                     new SecureRandom()); // random number generator
        HttpsURLConnection.setDefaultSSLSocketFactory(ssl_ctx.getSocketFactory());
    } catch(NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch(KeyManagementException e) {
        e.printStackTrace();
    }

关于javax.net.ssl.SSLHandshakeException 而来自 java 代码的 HttpsURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59034473/

相关文章:

java - 在 HashMap 中查找值的数量?

node.js - 与应用程序一起分发 Google API 凭据是否安全?

docker - ssl 证书或 nginx 代理服务器不工作

javascript - Digitalocean 使用 nginx 部署 Node https 应用程序

java - 使用哪种模式?

java - 模式和匹配器在 Java 中不起作用

android - QT 和 Android 8.0 : Error creating SSL context () 问题

RESTful 网络请求和用户事件跟踪网站

security - HTTPS 和基于 SSL 的 TCP 有什么区别

Java8 : Why a lambda expression could do a logical and(or) with boolean