简体   繁体   English

如何通过3G连接检查互联网连接状态?

[英]How to check internet connection status over 3G connection?

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. 当我以典型方式通过LAN电缆或Wi-Fi连接到Internet时,它可以正常工作。 When I'm using my 3G USB modem it returns false ( InternectConnectionProfile is null). 当我使用3G USB调制解调器时,它返回false( InternectConnectionProfile为null)。 Why is that? 这是为什么? How can I fix it? 我该如何解决?

You may consider pinging a server on the internet. 您可以考虑对Internet上的服务器执行ping操作。 This is not the 100% answer to your question but may be helpful from a different point of view. 这不是您问题的100%答案,但从不同的角度来看可能会有所帮助。

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))
    {
       // ...
    }
}

Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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