• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check the existing website(URL) is still working?

 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I want to create java program to check list of website(URL), to see if they are still working, if not working, send a notice (email) (or make is simple to print) the error message of why it is not working.
Now, these are some of my codes, I am stuck at, when a website is not working, how can I get the responseCode (the code I wrote is not working), and how to get the error message of what is wrong with that website(URL) which is not working.
As well, it seem I am on purpose write a wrong URL, the code run long time, it seems I used a wrong setconnectTimeout.
Any input will be appreciated.

 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpURLConnection has a method called getErrorStream() that you can use to read the error page provided by the web server, in a same way that the getInputStream() method can be used to read the non-error content if the page is loaded properly.
 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the quick response,
I will apply getErrorStream in the code.
Another question as I mentioned in the post about setReadTimeOut method, I set it in the code, how come I get long time to finish the running with the result of: java.net.SocketException: Connection reset.
It seems the setReadTimeOut I set is not working properly, as we do not want to program keep trying to open a unvalid URL, we want to it a limited time (for example 5 seconds or 10 seconds), if there is not response, then treat it as something wrong with that URL.
Should I use some other method instead of setReadTiemOut method to achieve my request^
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try setting the connection timeout as well.
 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Try setting the connection timeout as well.


Thanks a lot for the quick help,
Using both setConnect and setRead seem work.

Now new question: pelease see my code below:
Baiscally, I added the errorStream message on catch of second try, but it never be executed, instead, I got print on the catch of first try:
} catch(java.io.IOException fnf) {
System.out.println("IO input is wrong: " + fnf );
//serverInput = new BufferedReader( new InputStreamReader (con.getErrorStream()));
I even tried to add getErrorSteam in second catch of first try, compile error as the vaiable are defined in the try block, so the code within catch does not recoganize it. How to make it recognized if I add getErrorStream (the variable of try block to be recganizable in catch block).


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!