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

时间:2012-06-22 21:23:19

标签: 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行引发异常:

  

线程“main”中的异常javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径       在sun.security.ssl.Alerts.getSSLException(未知来源)       at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)       在sun.security.ssl.Handshaker.fatalSE(未知来源)       在sun.security.ssl.Handshaker.fatalSE(未知来源)       at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)       在sun.security.ssl.ClientHandshaker.processMessage(未知来源)       在sun.security.ssl.Handshaker.processLoop(未知来源)       在sun.security.ssl.Handshaker.process_record(未知来源)       at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)       at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)       在sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)       在sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)       在sun.net.www.protocol.https.HttpsClient.afterConnect(未知来源)       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)

2 个答案:

答案 0 :(得分:1)

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

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

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

中的问题

答案 1 :(得分:0)

如果您的客户端是用Java编写的,那么您需要做的就是将服务器证书添加到受信任的Java密钥存储区。

参考此页面。 https://blogs.oracle.com/gc/entry/unable_to_find_valid_certification