[英]How to check internet connection status over 3G connection?
我有以下方法:
public static bool IsNetworkConnected()
{
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
IReadOnlyList<ConnectionProfile> connectionProfile = NetworkInformation.GetConnectionProfiles();
if (InternetConnectionProfile == null)
return false;
else
return true;
}
当我以典型方式通过LAN电缆或Wi-Fi连接到Internet时,它可以正常工作。 当我使用3G USB调制解调器时,它返回false( InternectConnectionProfile
为null)。 这是为什么? 我该如何解决?
您可以考虑对Internet上的服务器执行ping操作。 这不是您问题的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))
{
// ...
}
}
尝试
public static bool IsConnectedToInternet()
{
ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile();
return (connectionProfile!=null && connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.