views:

671

answers:

2

We're working on a wrapped WebBrowser component. We'd like to display one page (e.g. oursite.com/thispage.html) if the user is online and another page (e.g. C:\somewhere\thispage_offline.html) if the user is offline. I'm able to to properly display both pages, but my issue is detecting the online/offline status.

I tried WebBrowser.IsOffline; however it seems that only relays the Offline Mode status, not whether or not the computer is actually able to reach the internet.

Is there a way to detect this from the WebBrowser component? Is there a way at all?

Thanks for all your help!

+2  A: 

What if you just used the Ping class to see if the site is available?

David
+5  A: 

The simplest way to check for internet connectivity is to ping your server.

Try this code:

public static bool IsOnline() {
    var pinger = new Ping();

    try {
        return pinger.Send("oursite.com").Status == IPStatus.Success;
    } catch(SocketException) { return false; } catch(PingException) { return false; }
}

Alternatively, you can try using the WebRequest class to send a request to a simple page on your site and see whether it succeeds. This is a better option because it will also make sure that IIS (or Apache) is running on the server.

SLaks
Ping didn't work for our server (though it worked for my personal site...weird), but WebRequest did. Thanks!
NickAldwin