简体   繁体   中英

URLConnection does not respect ReadTimeout

I'm using URLConnection in my code. I've set ReadTimeout to 5 seconds, but it doesn't come to effect. When I switch the wifi off the IO Exception is being called immidiately.

 URLConnection conn;     
 conn = new URL(StringUrls[0]).openConnection();
 conn.setReadTimeout(5000);
 in = conn.getInputStream();


      try {
           len = in.read(buffer);
           bufOutstream.write(buffer, 0, len);
           bufOutstream.close();
         } catch (IOException e1) {
             e1.printStackTrace();
                        }

IO Exception:

12-17 12:41:35.332  12761-13268/com.app.example W/System.err﹕ java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
12-17 12:41:35.362  12761-13268/com.app.example W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:542)
12-17 12:41:35.362  12761-13268/com.app.example W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
12-17 12:41:35.362  12761-13268/com.app.example W/System.err﹕ at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
12-17 12:41:35.362  12761-13268/com.app.example W/System.err﹕ at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
12-17 12:41:35.362  12761-13268/com.app.example W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
12-17 12:41:35.362  12761-13268/com.app.example W/System.err﹕ at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
12-17 12:41:35.362  12761-13268/com.app.example W/System.err﹕ at libcore.net.http.UnknownLengthHttpInputStream.read(UnknownLengthHttpInputStream.java:41)
12-17 12:41:35.362  12761-13268/com.app.example W/System.err﹕ at java.io.InputStream.read(InputStream.java:163)

example code as blow,

it will try max 20 times to read the correct data.

if exception occurs in one time,it just try again after 5 seconds.

private static int currentRetryCount = 0;

public static void main(String[] args) throws InterruptedException {
    retryRead(20);
}

public static void retryRead(int maxRetryCount) throws InterruptedException {
    if (currentRetryCount <= maxRetryCount) {
        try {
            System.out.println("try count " + currentRetryCount);
            URLConnection conn;
            conn = new URL("").openConnection();
            conn.setReadTimeout(5000);
            conn.getInputStream();
            // if nothing happens,it just return
            return;
        } catch (Exception e) {
            // if exception raises,it will try again after 5 seconds
            Thread.sleep(5000);

            currentRetryCount++;
            retryRead(maxRetryCount);
        }
    }
}

Maybe you're looking for setConnectTimeout ?

connectTimeout is the timeout for establishing the connection.
readTimeout is the timeout for reading data from the input stream after the connection has been established.

Also, the reason you get IOException immediately when your Wifi is off is because the connection gets aborted immediately regardless of the timeout if you have no active media for connection.

What effect do you want?Can you show more detail information ?

Set ReadTimeOut To N Seconds ,does not mean total read time will less than N seconds.

It mean's the max time between 2 data packages(By a connection ,you will get a lot of data packages) .

for example ,if there is no data available in N seconds,than the SocketTimeoutException will be raised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM