所以,我有一些看起来像这样的代码:
try {
URL url = new URL(url_raw);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
/*snip*/
connection.connect();
int response_code = connection.getResponseCode(); <-- Error here
/*snip*/
错误日志在这里:
W/System.err﹕ javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5f5d07c0: Failure in SSL library, usually a protocol error
W/System.err﹕ error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:744 0x5d908d74:0x00000000)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:449)
W/System.err﹕ at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:107)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)
我可以通过永远不会返回204来解决这个问题,但它看起来似乎太过分了= /
答案 0 :(得分:0)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:449)
W/System.err﹕ at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:107)
根据此跟踪,它在初始TLS握手中失败。必须在发送HTTP请求之前和读取HTTP响应之前完成HTTPS连接的TLS握手。由于它已经在TLS握手中失败,因此在收到状态代码之前,问题与状态代码本身无关。
相反,您需要查看服务器端的实现错误,而不是客户端。我的猜测是服务器坏了,在204的情况下不进行任何TLS握手,但鉴于没有提供有关此服务器的信息,这只是一个疯狂的猜测。