简体   繁体   English

Windows Phone 8连接检查WebClient抛出可怕的System.Net.WebException

[英]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.

相关问题 Windows Phone 8上未拦截System.Net.WebException - System.Net.WebException not intercepted on Windows Phone 8 WebClient SSL 错误:“System.Net.WebException:无法建立 SLL 连接” - WebClient SSL Error: "System.Net.WebException: The SLL connection could not be established" 多线程WebClient请求返回错误-System.Net.WebException - Multithreaded WebClient requests return error - System.Net.WebException HttpWebRequest引发System.Net.WebException Section = ResponseStatusLine - HttpWebRequest throws System.Net.WebException Section=ResponseStatusLine System.Net.WebException:'错误:ConnectFailure(连接被拒绝)' - System.Net.WebException: 'Error: ConnectFailure (Connection refused)' 在 Windows 7 WPF 客户端上发布异步抛出 System.Net.WebException - Post async throw System.Net.WebException on Windows 7 WPF client 使用 WebClient 时出现 System.Net.WebException:无法创建 SSL/TLS 安全通道 - System.Net.WebException when using WebClient: Can not create SSL/TLS secure channel 访问WebClient时出现“ System.Net.WebException”。 在浏览器上工作正常 - 'System.Net.WebException' when accessing WebClient. Works fine on browser System.dll中的System.net.webexception - System.net.webexception in system.dll system.net.webexception C#Vista - system.net.webexception C# Vista
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM