【问题标题】:Response failing over HTTPS in Java响应在 Java 中通过 HTTPS 失败
【发布时间】:2012-10-15 21:56:01
【问题描述】:

我们会在网站上运行爬虫,以使我们的信息保持最新。像one 这样的一些网站允许使用浏览器进行标准导航,但在使用标准 Java 库时它们不会发送响应。这是我用来访问主页(索引)页面的代码,

    try
    {
        URL my_url = new URL("https://www.capitallightingfixture.com");

        URLConnection u = my_url.openConnection();

        if ( u instanceof HttpURLConnection )
        {
            HttpURLConnection http_u = (HttpURLConnection)u;

            System.out.println(http_u.getResponseCode());
        }
    }
    catch (Exception 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

根据我对这些错误的了解,当 Java 无法验证服务器提供的证书时会抛出这些错误。但是,正如我上面提到的,谷歌浏览器在导航到页面时没有问题,它甚至会验证证书(不会引发安全错误)。

有没有办法可以覆盖 Java 并让它接收响应?

【问题讨论】:

标签: java ssl https


【解决方案1】:

我认为您尝试访问的 ip 正在使用自签名证书。请尝试以下两个链接。

How can I use different certificates on specific connections?

Using Self Signed certificate in java

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 2014-05-22
    • 2019-05-10
    • 2014-02-14
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多