响应在Java中通过HTTPS失败 [英] Response failing over HTTPS in Java

查看:229
本文介绍了响应在Java中通过HTTPS失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We run crawlers on websites to keep our info up to date. Some websites like this one allow standard navigation with the browser, but they don't send responses when using standard Java libraries. Here's the code I'm using to access the home (index) page,

    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();
    }

运行时,此程序将抛出以下错误

When run, this program will throw the following error

javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效请求目标的证书路径

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

From what I understand about the errors, they're thrown when Java can't validate the certificate provided by the server. However, as I mentioned above, Google Chrome has no problems navigating to the page, and it even verifies the certificate (no security errors are thrown).

有没有办法我可以覆盖Java并允许它接收响应吗?

Is there a way I can override Java and allow it to receive the response anyway?

推荐答案

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

I think the ip you are trying to access is using the self signed certificate. Try the following two links.

如何在特定连接上使用不同的证书?

在java中使用自签名证书

这篇关于响应在Java中通过HTTPS失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆