Response failing over HTTPS in Java

社会主义新天地 提交于 2020-01-03 04:15:09

问题


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 path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

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).

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


回答1:


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

How can I use different certificates on specific connections?

Using Self Signed certificate in java



来源:https://stackoverflow.com/questions/13073285/response-failing-over-https-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!