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/