2

我用 Java 编写了一个小代码来访问 URL。该代码适用于 HTTP 连接,但适用于 HTTPS(错误粘贴在下面)

任何解决此问题的帮助将不胜感激。

代码 :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class ConnectToWCProd {

    public static void main(String[] args) throws Exception {

        String userPassword = "user1:user1";
        String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());

        //URL oracle = new URL("http://<URL>/");
        URL oracle = new URL("https://<URL>");
        URLConnection yc = oracle.openConnection();

        yc.setRequestProperty("Authorization", "Basic " + encoding);

        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }

}

错误 :

线程“主”javax.net.ssl.SSLHandshakeException 中的异常:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在 com.sun 中找到请求目标的有效证书路径.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source) at com.sun.net.ssl.internal.ssl.Handshaker .fatalSE(Unknown Source) at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source) at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source) at com.sun .net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source) at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source) at com.sun.net.ssl.internal.ssl。Handshaker.process_record(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at com. sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.net.www.protocol.https.HttpsClient。 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) 上 sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 上 sun.net.www.protocol 上的 afterConnect(Unknown Source)。 https.HttpsURLConnectionImpl.getInputStream(Unknown Source) at ConnectToWCProd.main(ConnectToWCProd.java:21) 原因:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在 sun.security.validator.PKIXValidator.engineValidate(Unknown Source) 在 sun.security.validator.PKIXValidator.doBuild(Unknown Source) 上找到请求目标的有效证书路径。 security.validator.Validator.validate(Unknown Source) at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source) at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source ) at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source) ... 13 更多原因:sun.security.provider.certpath.SunCertPathBuilderException:无法在 sun 处找到请求目标的有效认证路径.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source) at java。security.cert.CertPathBuilder.build(Unknown Source) ... 19 更多

4

2 回答 2

1

阅读此链接

您必须在jre/lib/security/cacerts(默认路径)中包含证书

于 2013-03-22T11:32:25.710 回答
1

这可能对您有帮助:

http://www.java-samples.com/showtutorial.php?tutorialid=210

于 2013-03-22T11:34:31.783 回答