windows mobile:检查网络是否可用

时间:2009-12-26 21:06:42

标签: windows-mobile

如何在Windows Mobile 6.5+设备上查看互联网是否可用?

感谢。

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:1)

这是一段源自同一网络广播的代码。用法应该非常简单。虽然它可以作为网络监视器,但在检测到连接可用性时通知您,您可以选择仅提取IsUrlAvailable函数。

using System;
using Microsoft.WindowsMobile.Status;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Threading;

namespace RAMIS2.Classes {
    class NetworkWatcher : IDisposable {

        public delegate void NetworkStatusDeterminedDelegate(bool remoteServerReachable, bool internetReachable);
        public event NetworkStatusDeterminedDelegate NetworkStatusDetermined;

        SystemState nw = null;
        const string _remoteServerExpectedText = @"RAMIS";
        Uri InternetUrl = new Uri(@"http://www.google.com");
        const string _internetReachableExpectedText = @"Google";

        const int sleepTimeBetweenConnectTries = 15000;
        const int maxConnectTryCount = 2;
        Uri _remoteServerUrl = new Uri("https://demo.mywebsite.com/test.txt");

        bool internetReachable;
        bool remoteServerReachable;

        Thread bgw;

        public void Dispose() {
            if (bgw != null) { bgw.Abort(); bgw = null; }
            if (nw != null) { nw.Changed -= NetworkWatcher_Changed; nw.Dispose(); nw = null; }
        }

        Control invoke_ctrl;
        public NetworkWatcher() {
            invoke_ctrl = new Control();
        }

        public void Start() {
            // This only works for Windows Mobile 5.0 and higher, not PPC 2003
            if (Environment.OSVersion.Version >= new Version(5, 0)) {
                nw = new SystemState(SystemProperty.ConnectionsCount, false);
                nw.Changed += new ChangeEventHandler(NetworkWatcher_Changed);
            }
            bgw = new Thread(DetermineNetworkState);
            bgw.Start();
        }

        void NetworkWatcher_Changed(object sender, ChangeEventArgs args) {
            // instantly test new connection status
            if (bgw != null) DetermineNetworkState();
        }

        public bool IsUrlReachable(Uri url, string expectedText) {
            bool isUrlReachable = false;
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;
            Stream responseStream = null;
            StreamReader responseReader = null;

            for (int connectTryCount = 0;
                !isUrlReachable && connectTryCount = 0;
                }
                catch (SystemException) { }
                finally {
                    if (responseReader != null) responseReader.Close();
                    if (responseStream != null) responseStream.Close();
                    if (httpResponse != null) httpResponse.Close();
                }
                if (!isUrlReachable) Thread.Sleep(sleepTimeBetweenConnectTries);
            }
            return isUrlReachable;
        }

        void DetermineNetworkState() {
            try {
                while (true) {
                    if (Environment.OSVersion.Version >= ST.WM5.Version) DetermineNetworkStateInternal(SystemState.ConnectionsCount);
                    else DetermineNetworkStateInternal(1);
                    // invoke on GUI thread
                    invoke_ctrl.BeginInvoke(new NetworkStatusDeterminedDelegate(NetworkStatusDetermined), remoteServerReachable, internetReachable);
                    Thread.Sleep(sleepTimeBetweenConnectTries);
                }
            }
            catch (ThreadAbortException) { }
        }

        void DetermineNetworkStateInternal(int connectionsCount) {
            if (connectionsCount > 0) {
                remoteServerReachable = IsUrlReachable(_remoteServerUrl, _remoteServerExpectedText);
                internetReachable = remoteServerReachable ? true :
                    IsUrlReachable(InternetUrl, _internetReachableExpectedText);
            }
            else remoteServerReachable = internetReachable = false;
        }

    }
}