【问题标题】:Java URL Connection strange behavior on https URLs [duplicate]https URL上的Java URL连接奇怪行为[重复]
【发布时间】:2017-07-04 10:04:25
【问题描述】:
    public class URLConnectionReader {
        public static void main(String[] args) throws Exception {
            URL urlObj = new URL("https://www.paypal.com"); // enter url here
            HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            int code = connection.getResponseCode();
            System.out.println(code);
        }
}

我正在创建一个 URL 测试器来检查 URL 是否正常工作。上面是我的代码。当我用https://www.paypal.com 测试它时,它返回200,当我用https://www.facebook.com 测试它时,它抛出异常。 下面是异常堆栈跟踪

Exception in thread "main" 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:1949)
    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:1509)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    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.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at URLConnectionReader.main(URLConnectionReader.java:27)
Caused by: 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.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
    ... 11 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)

at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 17 more

谁能解释一下为什么https://www.paypal.com返回200但https://www.facebook.com抛出异常。

【问题讨论】:

    标签: java url urlconnection


    【解决方案1】:

    因为 paypal 的证书是由您的 trustStore 中的权威机构签署的,而 facebook 没有。

    【讨论】:

      【解决方案2】:

      这里的问题是 Java 默认带有非常有限的集合 根 CA 证书。它“接受”的 CA 比典型的少得多 浏览器。解决问题最简单的方法是导出一组 来自 Chrome 或 Firefox 等浏览器的 CA 证书并导入它们 使用 keytool 进入 Java 的密钥库。

      ref

      【讨论】:

      • 不要发布本质上是引用另一个问题的答案,而是投票或标记以关闭为重复。
      • 我在标志中看不到重复的标志
      • 我已经回滚了您上次的编辑,因为答案中的文字不是您的,因此必须引用和归因。您的编辑删除了它是引用的事实并删除了属性,这会使您的回答违反规则(特别是抄袭)。标记为重复可能需要稍高的声誉,不确定。
      • 我不知道我可以将帖子标记为重复。谢谢。我知道了
      • 仅供参考:@MarkRotteveel 我们需要 15rep 才能flag a post,仅此而已。 ebrahim poursadeqi,您可以在链接中查看有关该特权的更多信息。
      猜你喜欢
      • 1970-01-01
      • 2013-03-12
      • 2014-04-04
      • 2018-01-09
      • 2017-01-30
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多