Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is site available or not ?

 
moh sak
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i'm new in sockets and url's in java. and i need to know how to check if a given site lets say http://javaranch.com is down or not ?!! any ideas ?

i think it's an easy problem for persons who work on sockets and urls before


thanks in advance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a URL object and try to "get" a page from the server. The HTTP return codes will tell you whether the URL was bad, page was not found, the attempt timed out, etc. See if this SUN Tutorial gives you enough information to build that. If you get stuck, feel free to post some code and we'll see if we can help.
 
moh sak
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Stan i'll check it out

javaranch is my secod home and i allways feel free
 
moh sak
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again i'm back,

i open the link and read the urlConnection and the url , i made a simple program to check if the site is available or not heres the code :
public void run(){
long sleepPeriod = 60000;
while(true){
try {
URL url = new URL("http://sdfsd.com");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inRead = "";
cat.debug("Readed Data from the site");



// while((inRead=in.readLine()) != null){
// cat.debug(inRead);
// }
// if(in != null){
// cat.debug("Server OK");
// in.close();
// }
// else{
// cat.debug("No Such Url OR server Down");
// }


t.sleep(sleepPeriod);
}
catch (MalformedURLException ex) {
cat.debug("server down");
}
catch (IOException ioe) {
cat.debug("Input Output exception");
}
catch(InterruptedException interE){
cat.debug("Thread Interrupted");
}
}

}



as you can see i'm checking the site eery 1 min. using a thread , i write a wrong url and it acceps it and output that the server is ok.


i think it don't check if the site is available or not


help me
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code I used a few years ago. It wasn't production so I can't promise it does a great job of checking. It requires some content to come back, it has options to require a certain string in the results, eg "Welcome to my site", forbid a certain string in the results, eg "Error xxx", or to set a max elapsed time on the fetch. Apologies for the "old style" naming standards ... I don't do that any more.
 
moh sak
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again,

i was reading the HttpURLConnection class it has a constants values i think i should use them to know if the site is alve or not !???

can any one tell me how to use these constants and is it a good idea or not ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic