PDA

View Full Version : Java: How do I set a time out on the loading process of a webpage?


Sharagoz
11-10-2006, 05:16 AM
My program downloads webpages. It works fine as long as the url is valid. If the url is not valid it waits forever. How can I set a time out on the loading process?

Here's the part that loads the page:

try {
URL pageUrl = new URL(currentUrl);
BufferedReader reader = new BufferedReader(
new InputStreamReader(pageUrl.openStream() ) );
String line;
StringBuffer pageBuffer = new StringBuffer();
while ( (line = reader.readLine() ) != null ) {
pageBuffer.append( line );
}
String fetchedPage = pageBuffer.toString();
}
catch (Exception e) {
}

ess
11-10-2006, 11:43 AM
first...let me state the obvious...there is more than one way to do this. Of course..it all depends on what you want to achieve in the end.

First solution.
You can use threads. where you initiate the thread to open a web page...and then interrupt it (kill it) when a specified time has elapsed.

Second Solution.
You can make use of javax.swing.Timer, and schedule a delay before stopping the execution.

i am sure that there are other ways to do this.

Good luck
ess