Download Timeout URL Content

I want to load the contents of a url in java with the specified load time. For example: I want the maximum download timeout to be 10 seconds for www.yahoo.com. If the download takes more than 10 seconds, then you should throw an error. I wrote code to open a connection and download all the content. But how to set download timeout? Here is the code snippet:

        StringBuilder text = new StringBuilder();

        urlconn = (HttpURLConnection)url.openConnection();
        urlconn.setConnectTimeout(100000);
      //urlconn.setInstanceFollowRedirects(false);
        urlconn.setRequestMethod("GET");
        urlconn.connect();
        buf = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); 
        while((line = buf.readLine()) != null)
            text.append(line);
        System.out.println(url + "=> "+ urlconn.getResponseCode());
+3
source share
2 answers

You can install it URLConnection#setReadTimeout().

urlconn.setReadTimeout(10000); // 10 sec
// ...
+3
source

All Articles