aspose file tools*
The moose likes Struts and the fly likes ping a website Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ping a website" Watch "ping a website" New topic
Author

ping a website

sri kris
Greenhorn

Joined: May 23, 2008
Posts: 10
how do i ping a website thorugh java??
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Sri,

you should take a look at the method isReachable() of class InetAddress. I think this may help you

Marco
sri kris
Greenhorn

Joined: May 23, 2008
Posts: 10
but it returns true even if it is an invalid site. Any Other suggestions??
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8994
    
    9

You asked how to ping. Ping just tells you if there's a route to the host. It doesn't tell you the state of the services.
If you want to know if a web site is serving pages, java.net.URL can be used to retrieve documents.


[How To Ask Questions On JavaRanch]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Aaah, thanks for clarification, Joe! I just read "ping" and didn't think about the word "website"

@sri: Note that a "ping" is working on a different network and protocol layer than "web surfing", as Joe already mentioned. With a ping you can only check if a host (IP address) is reachable on the IP layer. The WWW service in contrast uses the HTTP protocol which in turn uses the TCP protocol on port 80. Perhaps this helps you to figure out what you really want to check for!

Marco
sri kris
Greenhorn

Joined: May 23, 2008
Posts: 10
i have used the following code.



but when i try to run this i get the following error.

connectin
java.net.ConnectException: Connection refused: connect


Can anyone help me in rectifying the error. Thanks in advance.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Sri,

I've tested your code and it's working!

The most common reason for a "connection refused" error is, if a host is reachable but it doesn't offer the service (HTTP in your case) you want to use. But in this case I don't think this is the reason unless someone has shutdown Microsoft's web servers

If a firewall would block the HTTP connections you'd usually get another error or just a timeout and it wouldn't be possible to surf on the internet from the host you're testing this program.

The only reason I can imagine is, that you are behind a proxy server! Do you try this at home or at work? Perhaps you can check if you have configured a proxy in the settings for your browser?

Marco
sri kris
Greenhorn

Joined: May 23, 2008
Posts: 10
Thanks a lot Marco.
i'm trying this at my work. if you dont mind can you please let me know how the browsers setting should be.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

The problem are not the browser settings! They are surely correct as you wouldn't be able to look at this forum here if they were not

But if you're behind a proxy then you'll have to use this proxy from your Java program, too. Unfortunately I don't know ad-hoc how to use a HTTP proxy with Java. So the best would probably be for you to check with your network administrator (or someone else who is responsible for the network) if you need to use a proxy server at all and what credentials you'll have to use if it's a authentication based proxy.

Marco
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8994
    
    9

Originally posted by Marco Ehrentreich:
Aaah, thanks for clarification, Joe!


Between the two of this, we'll figure this out.
Sri, have a look at the JDK Release Notes: Java Networking and Proxies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ping a website