Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ping a website

 
sri kris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i ping a website thorugh java??
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sri,

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

Marco
 
sri kris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it returns true even if it is an invalid site. Any Other suggestions??
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic