aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes is site available or not ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "is site available or not ?" Watch "is site available or not ?" New topic
Author

is site available or not ?

moh sak
Ranch Hand

Joined: May 11, 2005
Posts: 78
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
moh sak
Ranch Hand

Joined: May 11, 2005
Posts: 78
thanx Stan i'll check it out

javaranch is my secod home and i allways feel free
moh sak
Ranch Hand

Joined: May 11, 2005
Posts: 78
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: May 11, 2005
Posts: 78
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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is site available or not ?
 
Similar Threads
HttpURLConnection question
info needed
trouble retrieving html of all web sites
applet LoadAGif can't start: error????
difference between