views:

809

answers:

10

I don't want to tell the hard way that the internet is unavailable when I catch an exception from url.openStream().

Is there a simple way to tell if the computer is connected to the internet in Java? In this scenario, "connected to the internet" means being able to download data from a specific url.

If I try to download from it and it is not available, then the program hangs for a bit. I dont want that hanging. Therefore, i need a fast way of querying whether the website is available or not.

+2  A: 

You might be able to tell if a compouter is connected to a network but even if it is there's no guarantee the networking is working or that it's connected to the internet.

This is one of those "suck it and see" problems.

What's wrong with trying to connect? And why are you concerned whether or not they're connected to the internet?

cletus
+1  A: 

There is no such thing as "Internet availability". Imagine that your HTTP request go through a transparent HTTP proxy, you are trying to access a blacklisted site and you get a HTTP response from the proxy server telling you about access denied. Is Internet available in this scenario or not?

I think you shall be more specific about your problem.

Anonymous
A: 

You could send an ICMP message to a very well known host...

dicroce
A lot of networks, particularly corporate networks, block ICMP traffic.
cletus
@cletus he said a very "well known"... so it's supposed to be one host that allows ICMP traffic :)
rogeriopvl
A: 

You can check for a NTP server if your firewall does not forbids.

Any way I think the best option is to try to open an URL as you try to avoid, because is the service most commonly to be opened.

You can check for one or two dynamic URL that change constantly. For instace, some web that shows time. For instance this one i found googleing. More than one because the sites can be down ;-)

FerranB
+1  A: 

I don't know much about the low level HTTP plumbing available in Java, but you could always issue an http HEAD request for the url in advance. This causes the server to send back only the headers and not the data. With a 1-3 second timeout, you should be able to avoid any lengthy delays.

Chris
+2  A: 

If the criteria is whether you can access a given web server the only way to find out, is to access that web server.

You can, however, do it in a background thread at start up so the application is not delayed by it. Then the "yes/no" answer is available when the actual downloading is desired. Put a message in the status line so the user knows what is going on and is not suprised about uninitiated "connect to network" if s/he is not connected when your program is started.

Thorbjørn Ravn Andersen
A: 

I always ping www.yahoo.com.

Mark Harrison
+8  A: 

The problem you are trying to avoid is waiting for for your http connection to determine that the URL you are trying to access is really unavailable. In order to achieve this you need to stop using url.openStream() which is a shortcut for openConnection().getInputStream() and get some finer control over your connection.

URLConnection conn = url.openConnection();  
conn.setConnectTimeout(timeoutMs);  
conn.setReadTimeout(timeoutMs);  
in = conn.getInputStream();

This code will allow you to timeout the connection attempt if either the connection or the read exceeds the time you provide in the timeoutMs paramater.

Steve Weet
this is only a mediocre solution because you will still get a "hang" unless you do this in a separate thread.
Chii
Can you suggest a better one? I am already using a separate thread to download the data.
Penchant
+1  A: 

If you program is hanging while waiting to download then you are probably using the GUI thread to do the download. If you use a background thread, your program won't hang even if it take a long time to timeout/download.

Peter Lawrey
A: 

See "How can I detect if my app is offline?" at http://lopica.sourceforge.net/faq.html#offline.