我正在使用 HttpURLConnection 向网站发送 POST 以登录用户。我的代码:
byte[] queryBytes = query.toString().getBytes();
/* Open the connection to the login page */
URL url = new URL( urlString );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout( CONNECTION_TIMEOUT );
connection.setReadTimeout( CONNECTION_TIMEOUT );
connection.setRequestProperty( "Accept", "text/html, application/octet-stream" );
connection.setRequestProperty( "Cache-Control", "max-age=0" );
connection.setFixedLengthStreamingMode( queryBytes.length );
connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
connection.setRequestProperty( "Origin", ORIGIN );
connection.setRequestProperty( "Referer", REFERER );
/* Add POST data string */
connection.setDoOutput( true );
outputStream = connection.getOutputStream();
outputStream.write( queryBytes );
outputStream.close();
/* Execute the POST connection */
int code = connection.getResponseCode();
这会在最后一行使用以下 logcat 导致 IOException:
06-15 12:03:53.285: E/MyApp(1856): Login: IOException: Read timed out
06-15 12:03:53.285: E/MyApp(1856): java.net.SocketTimeoutException: Read timed out
06-15 12:03:53.285: E/MyApp(1856): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method)
06-15 12:03:53.285: E/MyApp(1856): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:675)
06-15 12:03:53.285: E/MyApp(1856): at libcore.io.Streams.readSingleByte(Streams.java:41)
06-15 12:03:53.285: E/MyApp(1856): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:659)
06-15 12:03:53.285: E/MyApp(1856): at libcore.io.Streams.readAsciiLine(Streams.java:201)
06-15 12:03:53.285: E/MyApp(1856): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
06-15 12:03:53.285: E/MyApp(1856): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
06-15 12:03:53.285: E/MyApp(1856): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
06-15 12:03:53.285: E/MyApp(1856): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
06-15 12:03:53.285: E/MyApp(1856): at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
06-15 12:03:53.285: E/MyApp(1856): at com.myapp.interfaces.SkyWestInterface.login(SkyWestInterface.java:606)
06-15 12:03:53.285: E/MyApp(1856): at com.myapp.services.MonthUpdateService.processIntent(MonthUpdateService.java:181)
06-15 12:03:53.285: E/MyApp(1856): at com.myapp.services.UpdateService.onHandleIntent(UpdateService.java:237)
06-15 12:03:53.285: E/MyApp(1856): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
06-15 12:03:53.285: E/MyApp(1856): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 12:03:53.285: E/MyApp(1856): at android.os.Looper.loop(Looper.java:137)
06-15 12:03:53.285: E/MyApp(1856): at android.os.HandlerThread.run(HandlerThread.java:60)
如果我删除该connection.setFixedLengthStreamingMode( queryBytes.length );
行,那么它可以工作。为什么这条线会导致超时?是因为 SSL 吗?