views:

151

answers:

2

How do I check if the internet is available on a windows mobile 6.5+ device?

Thanks.

+2  A: 

Something like this. In essence, this is from the MSDN Tips and Tricks webcast of January 12, 2005. It's slightly rewritten to fit my own standards and completely untested, but you get the idea. Use as you please.

enum ConnectionStatus
{
    Offline,
    OnlineTargetNotFound,
    OnlineTargetFound,
}

ConnectionStatus GetConnectionStatus(String url)
{
    try
    {
        //
        // If the device is set to loopback, then no connection exists.
        //
        String hostName = System.Net.Dns.GetHostName();
        System.Net.IPHostEntry host = System.Net.Dns.GetHostByName(hostName);
        String ipAddress = host.AddressList.ToString();

        if(ipAddress == System.Net.IPAddress.Parse("127.0.0.1").ToString())
        {
            return ConnectionStatus.Offline;
        }

        //
        // Now we know we're online. Use a web request and check
        // for a response to see if the target can be found or not.
        // N.B. There are blocking calls here.
        //
        System.Net.WebResponse webResponse = null;
        try
        {
            System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
            webRequest.Timeout = 10000;
            webResponse = webRequest.GetResponse();

            return ConnectionStatus.OnlineTargetFound;
        }
        catch(Exception)
        {
            return ConnectionStatus.OnlineTargetNotFound;
        }
    }
    catch(Exception)
    {
        return ConnectionStatus.Offline;
    }
}
Johann Gerell
+1  A: 
Frank Razenberg