如何在不同的班级中获得互联网检查的结果
本文关键字:互联网 结果 检查 | 更新日期: 2023-09-27 18:09:54
在我的应用程序中检查互联网连接和对我的网站的访问。我在另一节课上得不到成绩。有一个通过localSettings.Values的解决方案,但这不是我想要的。请帮帮我。谢谢
public class NetworkUtils{
public enum ConnType
{
CONN_MOBILE,
CONN_WIFI,
CONN_WAN,
CONN_NO,
CONN_MY
}
public ConnType GetConnectionGeneration()
{
string connectionProfileInfo = string.Empty;
try
{
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (InternetConnectionProfile == null)
{
NotifyUser("Not connected to Internet'n");
return (ConnType.CONN_NO);
}
else
{
if (InternetConnectionProfile.IsWlanConnectionProfile)
{
HttpClient httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(0, 0, 0, 0, 300);
try
{
httpClient.GetAsync(new Uri("http://www.myexample.com")).Wait();
NotifyUser("MY SITE connected'n");
return (ConnType.CONN_MY);
}
catch (Exception ex)
{
NotifyUser("Unexpected exception occurred: " + ex.ToString());
}
return (ConnType.CONN_WIFI);
}
else if (InternetConnectionProfile.IsWwanConnectionProfile)
{
WwanDataClass connectionClass = InternetConnectionProfile.WwanConnectionProfileDetails.GetCurrentDataClass();
switch (connectionClass)
{
//2G-equivalent
case WwanDataClass.Edge:
case WwanDataClass.Gprs:
//3G-equivalent
case WwanDataClass.Cdma1xEvdo:
case WwanDataClass.Cdma1xEvdoRevA:
case WwanDataClass.Cdma1xEvdoRevB:
case WwanDataClass.Cdma1xEvdv:
case WwanDataClass.Cdma1xRtt:
case WwanDataClass.Cdma3xRtt:
case WwanDataClass.CdmaUmb:
case WwanDataClass.Umts:
case WwanDataClass.Hsdpa:
case WwanDataClass.Hsupa:
//4G-equivalent
case WwanDataClass.LteAdvanced:
return (ConnType.CONN_MOBILE);
//not connected
case WwanDataClass.None:
return (ConnType.CONN_NO);
//unknown
case WwanDataClass.Custom:
default:
HttpClient httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(0, 0, 0, 0, 300);
try
{
httpClient.GetAsync(new Uri("http://www.myexample.com")).Wait();
return (ConnType.CONN_MY);
}
catch (Exception ex)
{
NotifyUser("Unexpected exception occurred: " + ex.ToString());
}
return (ConnType.CONN_WAN);
}
}
return (ConnType.CONN_MOBILE);
}
}
catch (Exception ex)
{
NotifyUser("Unexpected exception occurred: " + ex.ToString());
return (ConnType.CONN_NO);
}
}
}
如果我做了一些不太正确的事情。如果也有好的想法,我将不胜感激。我测试了一下,但什么也没发生。
public sealed partial class TestPage : Page
{
public TestPage()
{
InitializeComponent();
if (NetworkUtils.ConnType.CONN_MY.ToString().Equals("CONN_MY"){
TextBlock.Text = "GOOD Connection";
} else if (NetworkUtils.ConnType.CONN_WIFI.ToString().Equals("CONN_MY"){
TextBlock.Text = "WIFI Connection";
}
...
}
}
var generation = new NetworkUtils().GetConnectionGeneration();
swtich (generation)
{
case NetworkUtils.ConnType.CONN_MY:
{
//...
break;
}
case NetworkUtils.ConnType.CONN_WIF:
{
//...
break;
}
// othes cases
default:
// handle exceptional case
}
var gen = new NetworkUtils().GetConnectionGeneration();
if (gen == NetworkUtils.ConnType.CONN_MY) {
//...
}
问题是您正在检查枚举成员(CONN_MY
(的toString
,所以if语句的第一行始终为true,它将始终显示Good Connection
。你必须先打电话给GetConnectionGeneration()
。