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.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());
source
share