Java URL Timeout

I am trying to parse an XML file from an HTTP URL. I want to set a timeout of 15 seconds, if the XML fetch takes longer, I want to report a timeout. For some reason, setConnectTimeout and setReadTimeout are not working. Here is 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 a SocketTimeoutException.

Thank you, Chris

+46
java urlconnection timeout connect
Jul 02 2018-10-10T00:
source share
5 answers

Try the following:

  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 see how to use Jsoup: http://jsoup.org/cookbook/input/load-document-from-url

+25
Jan 08 2018-11-11T00:
source share

You can force disconnect from sleep mode. That's 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"); } } 
+10
04 Oct '13 at 13:01
source share

I used similar code to download logs from servers. I am debugging my code and found that the implementation of the URLConnection that returns is sun.net.www.protocol.http.HttpURLConnection.

The abstract class java.net.URLConnection has two connectTimeout and readTimeout attributes and setters in the abstract class. Believe it or not. Sun.net.www.protocol.http.HttpURLConnection have the same connectTimeout and readTimeout attributes without the setters and attributes from the implementation class that are used in the getInputStream method. Therefore, there is no need to set connectTimeout and readTimeout, because they are never used in the getInputStream method. In my opinion, this is a bug in the implementation of sun.net.www.protocol.http.HttpURLConnection.

My solution for this was to use HttpClient and Get queries.

+8
Sep 30 '11 at 13:43
source share

You can set timeouts for all connections made from jvm by changing the following system properties:

 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); 

Each connection will be disconnected after 10 seconds.

Setting "defaultReadTimeout" is not required, but is shown as an example if you need to control reading.

+8
Apr 28 '15 at 21:46
source share

Are you on windows? The main socket implementation on Windows does not seem to support the SO_TIMEOUT parameter very well. See Also this answer: setSoTimeout in the client socket does not affect the socket

+1
Nov 23 2018-11-11T00:
source share



All Articles