Below if the code I've been using for some time to get the connection status of the device that my store app in running on. What appears to have happened recently is, whilst it still finds the correct connection profile, it returns the level as Local, rather than Internet access.

IReadOnlyList<ConnectionProfile> p = NetworkInformation.GetConnectionProfiles();

foreach (ConnectionProfile prof in p)
{
    NetworkConnectivityLevel lev = prof.GetNetworkConnectivityLevel();
    if (lev == NetworkConnectivityLevel.InternetAccess)
    {
        return true;
    }
}
return false;

Can anyone tell me why this might be, and how I can persuade it that I do, in fact, have a working internet connection (which I can prove by being able to post this question :-) )?

有帮助吗?

解决方案

try this one

private bool roaming;
    private string connectionProfileInfo;

    private async Task<bool> IsConnectedToInternet()
    {
        HttpWebRequest webReq;
        HttpWebResponse resp = null;
       // HttpStatusCode respcode;
        Uri url = null;
        url = new Uri("http://www.dartinnovations.com");
        webReq = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            resp = (HttpWebResponse)await webReq.GetResponseAsync();
          //  Debug.WriteLine(resp.StatusCode);
            webReq.Abort();
            webReq = null;
            url = null;
            resp = null;
            return true;
        }
        catch
        {
            webReq.Abort();
            webReq = null;
            return false;
        }
    }

    private async Task<bool> CheckForConnection()
    {
        bool isConnected = await IsConnectedToInternet();
        Debug.WriteLine(isConnected);
        ConnectionProfile internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();



        if (isConnected)
        {
            if (internetConnectionProfile != null)//Gets metereing info, Connectionprofile gives false positives when used to check for internet connectivity
            {
                Debug.WriteLine("internet available");
                GetMeteringInformation(internetConnCectionProfile);
            }
            else
            {
                connectionProfileInfo = "Roaming information not available";
                roaming = false;
               // Debug.WriteLine("no connections");
            }
            return true;

        }           
        return false;

    }

  private async Task GetMeteringInformation(ConnectionProfile connectionProfile)
    {
        ConnectionCost connectionCost = connectionProfile.GetConnectionCost();

        roaming = connectionCost.Roaming;

        connectionProfileInfo = "Over Data Limit :" + connectionCost.OverDataLimit + " | Approaching Data Limit :" +
                                connectionCost.ApproachingDataLimit;
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top