如何在没有认证问题的情况下连接到 URL?

Posted

技术标签:

【中文标题】如何在没有认证问题的情况下连接到 URL?【英文标题】:How to connect to a URL without certification problems? 【发布时间】:2019-05-27 12:28:02 【问题描述】:

简介

我希望能够从网站检索源页面以处理数据。一些网络让我可以连接到它们,但有些网络让我很困惑unable to find valid certification path to requested target

我想提一下,我不拥有我从中抓取数据的网站*(因为我目前阅读的一些解决方案提到了服务器拥有的东西)*

另外,我还在学习中,有一些问题,不要太技术,我只是想把这个问题解决一下。

问题

Java 似乎不信任该网站,因此向我抛出了下面的异常。我在这里阅读了一些答案,但到目前为止没有任何效果。

到目前为止我做了什么

我将网站所需的证书导入为:

keytool -import -alias virto -file /path to crt/ -keystore virtocert

然后重新启动 PC,但仍然收到此异常错误。当我运行我的代码时。

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:1937)
    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:1478)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
    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:1050)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1512)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at HttpsClientUtil.main(HttpsClientUtil.java:23)
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:1460)
    ... 13 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:145)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    ... 19 more
C:\Users\Jonathan\Desktop\Java\WebScraper_03\WebScraper\nbproject\build-impl.xml:1339: The following error occurred while executing this line:
C:\Users\Jonathan\Desktop\Java\WebScraper_03\WebScraper\nbproject\build-impl.xml:980: Java returned: 1

代码

 public static void main(String[] args) throws Exception 
        String httpsURL = "https://virtonomics.com/";
        String FILENAME = "c:\\temp\\filename.html";
        BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME));
        URL myurl = new URL(httpsURL);
        HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
        con.setRequestProperty ( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" );
        InputStream ins = con.getInputStream();
        InputStreamReader isr = new InputStreamReader(ins, "Windows-1252");
        BufferedReader in = new BufferedReader(isr);
        String inputLine;

        // Write each line into the file
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
            bw.write(inputLine);
        
        in.close(); 
        bw.close();
    

我还需要做什么才能解决这个问题?

感谢您对这个问题感兴趣。

【问题讨论】:

【参考方案1】:

您可以设置自己的密钥库并验证证书,也可以选择完全忽略证书。

您可以查看此帖子以了解各种选项:Ignore SSL Certificate Errors with Java

【讨论】:

以上是关于如何在没有认证问题的情况下连接到 URL?的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有 SSL 的情况下连接到 MySQL 数据库

如何在没有 oAuth 身份验证的情况下连接到 Google Calendar API?

如何在不使用 *** 映像的情况下连接到 docker 中的 ***?

如何在不指定数据库名称的情况下连接到 PostgreSQL?

C#:如何在启用 SSL 的情况下连接到 Active Directory?

如何在启用 tls 的情况下连接到 nomad/consul UI?