【问题标题】:Getting internet connection status from WinRT从 WinRT 获取 Internet 连接状态
【发布时间】:2014-03-23 11:13:15
【问题描述】:

如果我已经使用了一段时间的代码来获取运行我的商店应用程序的设备的连接状态,则如下所示。最近似乎发生的事情是,虽然它仍然找到正确的连接配置文件,但它返回的级别为本地,而不是 Internet 访问。

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

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

谁能告诉我为什么会这样,以及我如何说服我确实有一个有效的互联网连接(我可以通过发布这个问题来证明:-))?

【问题讨论】:

    标签: c# windows-runtime microsoft-metro windows-store-apps


    【解决方案1】:

    试试这个

    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;
        }
    

    【讨论】:

    • 难以置信,这确实有效。 GetInternetConnectionProfile() 确实有效,但 GetConnectionProfiles() 失败。你知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多