[英]Windows Phone 8 connection check WebClient throws dreaded System.Net.WebException
I'm writing a Windows Phone 8 application that is designed to work online and offline. 我正在编写一个旨在在线和离线工作的Windows Phone 8应用程序。 To do this it needs to be able to check for an internet connection. 要做到这一点,它需要能够检查互联网连接。 This is fine if there's no connection, or if there is a connection, but if the user is connected to a wifi hotspot and they haven't authenticated, then I need to return a 'no connection' status. 如果没有连接,或者有连接,这很好,但如果用户连接到wifi热点但未经过身份验证,那么我需要返回“无连接”状态。 However, even with a try catch it seems impossible to catch the System.Net.WebException error using either WebClient or HttpWebRequest. 但是,即使使用try catch,使用WebClient或HttpWebRequest也无法捕获System.Net.WebException错误。 I've searched all over for solutions but none seem to work - any help would be appreciated. 我已经搜遍了所有的解决方案,但似乎没有工作 - 任何帮助将不胜感激。
Here's my connection check code: 这是我的连接检查代码:
public async Task<bool> tskbooCheckConnectionSimpleNoQueue()
{
bool booHasConnection = false;
bool isNetwork = NetworkInterface.GetIsNetworkAvailable();
if (!isNetwork)
{
booHasConnection = false;
IsolatedStorageSettings.ApplicationSettings["booHasConnection"] = false;
return booHasConnection;
}
else
{
//actually check connection
WebClient wcConnectionCheck = new WebClient();
string strData = "";
string strConnectionCheck = "https://urltocheckconnection.com/connectioncheckmethod";
try
{
strData = await wcConnectionCheck.DownloadStringTaskAsync(new Uri(strConnectionCheck)); //exception thrown here
if (strData.ToString().Contains("success"))
{
booHasConnection = true;
IsolatedStorageSettings.ApplicationSettings["booHasConnection"] = true;
return booHasConnection;
}
else
{
booHasConnection = false;
IsolatedStorageSettings.ApplicationSettings["booHasConnection"] = false;
return booHasConnection;
}
}
catch (WebException ex)
{
booHasConnection = false;
IsolatedStorageSettings.ApplicationSettings["booHasConnection"] = false;
return booHasConnection;
}
}
}
Sounds like you have catch handled exceptions turned on. 听起来你已经开始处理异常处理。
In visual studio, click the 'Debug' menu and select 'Exceptions'. 在visual studio中,单击“调试”菜单并选择“例外”。
Under the 'Common Language Runtime Exceptions' item, untick the 'Thrown' column. 在“Common Language Runtime Exceptions”项下,取消选中“Thrown”列。
Your debugger will no longer break, and the exception will be caught. 您的调试器将不再中断,并将捕获异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.