Thursday, December 4, 2014

SocketTimeoutException in Jsoup: Read timed out

While using jsoup library to request HTTP connection several times,

                while((line = br.readLine())!=null){  
                     Connection c = Jsoup.connect(line);  
                     c.followRedirects(true);  
                     Response r = c.execute(); 
                }

it will have SocketTimeoutException as below:

java.net.SocketTimeoutException: Read timed out

According to the Javadoc, the default timeout is 3sec, in order to get rid of the exception, you could set the timeout by yourself (A timeout of 0 means infinite)

                while((line = br.readLine())!=null){  
                     Connection c = Jsoup.connect(line);  
                     c.followRedirects(true);  
                     Response r = c.timeout(0).execute();  
             }  
Post a Comment