How to check internet connection status over 3G co

2019-02-16 00:42发布

问题:

I have the following method:

public static bool IsNetworkConnected()
{
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
    IReadOnlyList<ConnectionProfile> connectionProfile = NetworkInformation.GetConnectionProfiles();
    if (InternetConnectionProfile == null)
        return false;
    else
        return true;
}

It works fine when I'm connected to the internet in a typical way - via LAN cable or Wi-Fi. When I'm using my 3G USB modem it returns false (InternectConnectionProfile is null). Why is that? How can I fix it?

回答1:

You may consider pinging a server on the internet. This is not the 100% answer to your question but may be helpful from a different point of view.

using System.Net;
using System.Net.NetworkInformation;

using (Ping Packet = new Ping()) 
{
    if (Packet.Send(IPAddress.Parse(IP), 1000, new byte[] { 0 }).Status ==   IPStatus.Success))
    {
       // ...
    }
}


回答2:

Try

public static bool IsConnectedToInternet()
{
        ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile();
        return (connectionProfile!=null && connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
}


回答3:

This might help you http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/a7ba014c-3a31-4ff3-ba10-0c835305828b/