How can I implement a 60 second timeout in this code?
This code is opening a URL, downloading plain text, and sending output as a string variable.
Works, but sometimes hangs, and I have to start all over again.
I was hoping something which would timeout after 60 seconds and return whatever data is retrieved.
Please don't suggest to use external libs like Apache etc. If I could edit this code itself, then that would be better.
public static String readURL( URL url )
{
try
{
// open the url stream, wrap it an a few "readers"
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String s="";
String line="";
while ((line = reader.readLine()) != null)
{
s=s+"\r\n"+line;
}
reader.close();
return s;
}catch(Exception e){ StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); return errors.toString(); }
}//end method