Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java URLConnection Timeout

I am trying to parse an XML file from an HTTP URL. I want to configure a timeout of 15 seconds if the XML fetch takes longer than that, I want to report a timeout. For some reason, the setConnectTimeout and setReadTimeout do not work. Here's the code:

          URL url = new URL("http://www.myurl.com/sample.xml");           URLConnection urlConn = url.openConnection();           urlConn.setConnectTimeout(15000);           urlConn.setReadTimeout(15000);           urlConn.setAllowUserInteraction(false);                    urlConn.setDoOutput(true);            InputStream inStream = urlConn.getInputStream();           InputSource input = new InputSource(inStream); 

And I am catching the SocketTimeoutException.

Thanks Chris

like image 390
Chris Avatar asked Jul 02 '10 06:07

Chris


People also ask

How do you set a timeout for a URL?

Here's the code: URL url = new URL("http://www.myurl.com/sample.xml"); URLConnection urlConn = url. openConnection(); urlConn. setConnectTimeout(15000); urlConn.

What is default connection timeout in Java?

There are two timeout settings: Max Wait Time: Amount of time the caller (the code requesting a connection) will wait before getting a connection timeout. The default is 60 seconds.

What is URLConnection in Java?

The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL. Instances of this class can be used both to read from and to write to the resource referenced by the URL.

What is the default timeout for HttpURLConnection?

Appears the "default" timeouts for HttpURLConnection are zero which means "no timeout."


2 Answers

Try this:

       import java.net.HttpURLConnection;         URL url = new URL("http://www.myurl.com/sample.xml");         HttpURLConnection huc = (HttpURLConnection) url.openConnection();        HttpURLConnection.setFollowRedirects(false);        huc.setConnectTimeout(15 * 1000);        huc.setRequestMethod("GET");        huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");        huc.connect();        InputStream input = huc.getInputStream(); 

OR

       import org.jsoup.nodes.Document;         Document doc = null;        try {            doc = Jsoup.connect("http://www.myurl.com/sample.xml").get();        } catch (Exception e) {            //log error        } 

And take look on how to use Jsoup: http://jsoup.org/cookbook/input/load-document-from-url

like image 107
MatBanik Avatar answered Sep 20 '22 02:09

MatBanik


You can manually force disconnection by a Thread sleep. This is an example:

URLConnection con = url.openConnection(); con.setConnectTimeout(5000); con.setReadTimeout(5000); new Thread(new InterruptThread(con)).start(); 

then

public class InterruptThread implements Runnable {      HttpURLConnection con;     public InterruptThread(HttpURLConnection con) {         this.con = con;     }      public void run() {         try {             Thread.sleep(5000); // or Thread.sleep(con.getConnectTimeout())         } catch (InterruptedException e) {          }         con.disconnect();         System.out.println("Timer thread forcing to quit connection");     } } 
like image 31
kinghomer Avatar answered Sep 24 '22 02:09

kinghomer