java - 向服务器发送http请求有时会发生异常

标签 java ssl network-programming httpwebrequest

URL oauth = new URL(URL);
URLConnection oauth_connection = oauth.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(
            oauth_connection.getInputStream()));

String inputLine = "";

String line;

//System.out.println(in.readLine());
while((line = in.readLine()) != null)
    inputLine += line;

in.close();

BufferedReader 行抛出异常:

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(Unknown Source) at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) at sun.security.ssl.Handshaker.fatalSE(Unknown Source) at sun.security.ssl.Handshaker.fatalSE(Unknown Source) at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source) at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source) at sun.security.ssl.Handshaker.processLoop(Unknown Source) at sun.security.ssl.Handshaker.process_record(Unknown Source) at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)

最佳答案

您可能拥有自签名证书或链式证书,其中链的某些部分不受信任。

您可以将证书导入到您的信任库中,请参阅 Digital Certificate: How to import .cer file in to .truststore file using?

或者您可以忽略此问题中的问题 Ignore certificate errors when requesting a URL in Java

关于java - 向服务器发送http请求有时会发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11164324/

相关文章:

java - 在Java中生成一个随机素数

java - 在 android TextView 中检测新行

firefox - 'standalone' 网络应用程序中的 SSL sec_error,完整的浏览器正常

java - 在我的 mac osx 上的 intellij maven 中禁止父定义中的属性

java - 初始化 Jetty+Jersey

google-chrome - SSL Localhost 隐私错误

apache - 如何在 Apache + Tomcat 设置上设置 SSL

c - TCP套接字到多个IP/端口

java - 处理多线程事件

networking - 我的网络浏览器如何解析域名?