以下代码适用于 HTTP(API 版本 7 和 8)和 HTTPS(API 版本 7)。对于 HTTPS(API 版本 8),我收到错误“java.io.IOException:SSL 握手失败:SSL 库失败,通常是协议错误”
任何想法为什么仅在版本 8 上?
编码...
HttpURLConnection connection = null;
BufferedReader brd = null;
String line = "" ;
StringBuilder sb = new StringBuilder();
try {
//Set up the initial connection
//connection = (HttpURLConnection) new URL("http://MYSERVER.com").openConnection(); //Works
connection = (HttpURLConnection) new URL("https://MYSERVER.com").openConnection(); //error
connection.connect();
brd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
sb = new StringBuilder();
while ((line = brd.readLine()) != null)
sb.append(line);
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally{
//close the connection, set all objects to null
connection.disconnect();
brd = null;
sb = null;
connection = null;
}
完整的错误堆栈
WARN/System.err(267): java.io.IOException: SSL handshake failure: Failure in SSL library, 通常是协议错误 WARN/System.err(267): error:140773F2:SSLroutines:SSL23_GET_SERVER_HELLO:sslv3 警报意外消息(external/openssl/ssl/s23_clnt.c:599 0xaf076ad8:0x00000000) at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativeconnect(Native Method) at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl .startHandshake(OpenSSLSocketImpl.java:316) 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:168) 在 org.apache.harmony.luni.internal.net .www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:399) 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:147)
谢谢, 纳拉亚南