windows mobile:检查网络是否可用

Sha*_*ean 2 windows-mobile

如何检查Windows Mobile 6.5+设备上是否有互联网?

谢谢.

Joh*_*ell 6

像这样的东西.从本质上讲,这是来自2005年1月12日的MSDN提示和技巧网络广播.它略有改写,以符合我自己的标准,完全未经测试,但你明白了.随便使用.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)