简体   繁体   中英

Windows phone7: Check Internet connection availability

In windows phone7 I just need to check whether an internet connection is available before continue the next set of operations. I searched and found 3 options that suggested in many POSTS. They are as follows.

(a). bool isNetworkAvailable = NetworkInterface.GetIsNetworkAvailable();
(b). bool isNetworkAvailable = DeviceNetworkInformation.IsNetworkAvailable;
(c). bool isNetworkAvailable = (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None);

In (a) and (b) options even when the WIFI is OFF and no internet connection available, it provides TRUE as the result. But (c) provides the exact(real) result when internet is not available. But it takes so much time to respond. Until then the application is stucked...

I checked all these with a real device(Not with the emulator only) and struggling which way will be the best way to check internet availability of the device.

Any suggestion is highly appreciated... Thanks inadvance.....

There is a difference between knowing that the the network is available, and is connected to the wider Internet. NetworkInterface.GetIsNetworkAvailable() is pretty quick and will return false if the network interfaces are disabled.

If it returns that the the network is available, it is only indicating that you can connect - there will still be potential issues with speeds of DNS lookups, connection over the cellular network (if there is no WiFi connection), and the device is not already connected, etc.

You should consider moving the network access off of the UI thread if it is causing you performance problems.

Here we go use this. I have the same concept in my project and i have done the following. Hope it helps.

  public bool netWorkAvailable()
    {
        if (NetworkInterface.GetIsNetworkAvailable())
        {
            Logger.log(TAG, "netWorkAvailable()");
            return true;
        }
        return false;
    }

 if (netWorkAvailable())
   {
     buffer.Append(SERVER_URL);
     buffer.Append("_req=").Append(8);
     httpConnection = new HttpConnection();
     httpConnection.connect(REQ_REGISTRATION, buffer.ToString(), listener, null);
     httpConnection.Post();
    }

I have different class that handles all httpconnection. in addition to these i also check few other things for the purpose of my project and they are as follows:

public bool IsWifiAvailable()
    {
     if ((NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211))
         {
          return true;
         }
     else
       {
        return false;
       }
    }

    public bool threeGkAvailable()
    {
        if ((NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.MobileBroadbandCdma)
                  || (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.MobileBroadbandGsm))
        {
            return true;
        }
        else
        {
            return false;

        }
    }

Perhaps you might want to try sending a HttpWebRequest to a known webpage and checking whether the first few characters in the response matches the intended result. You might also want to loop through this check function a couple of times to the point where the connectivity test actually obtains a result (be it a success or failure).

Load this on a thread separated from the UI thread, and give it a go. Be sure to catch any exceptions (in the event that the test does not work out due to a lack of connectivity or other issues). I've actually used this in one of my Windows Phone 7 apps, but I'm not sure if it's the best of ideas.

You can check whether the user is connected to internet or not by requesting a webpage using webclient.

WebClient client = new WebClient();
        client.DownloadStringCompleted += (s, e) =>
        {
            if (e.Error == null && !e.Cancelled)
            {
                //completed.TrySetResult(true);
                MessageBox.Show("Internet Connected", "True", MessageBoxButton.OK);
            }
            else
            {
                //completed.TrySetResult(false);
                MessageBox.Show("Internet Not Connected", "False", MessageBoxButton.OK);
            }
        };
        client.DownloadStringAsync(new Uri("http://www.google.com/"));

Also you can set a timerout for 20 seconds in case if there is very

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM