如何在Windows Phone 8中捕获Web异常?

时间:2013-07-22 12:58:40

标签: windows-phone-8 wampserver webexception

我使用Wamp Server作为后端,C#作为前端。为了Cheeking目的,我关闭服务器。并运行该程序。它在输出窗口中抛出以下错误。

'TaskHost.exe' (CLR C:\windows\system32\coreclr.dll: Silverlight AppDomain): Loaded            

'C:\windows\system32\System.Core.ni.dll'. Skipped loading symbols. Module is optimized and    the debugger option 'Just My Code' is enabled.

 An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

 An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

我的目标是将Web异常捕获到消息框中。是否可能。

我已经使用try,catch语句捕获该异常。但它不起作用。

我的代码

 private void Login_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(this.username.Text) || string.IsNullOrWhiteSpace(this.password.Password))
        {
            MessageBox.Show("Please Enter the Username and Password");
            this.username.Text = "";
            this.password.Password = "";

        }
        else
        {

                string url = ob.localhost + "login_validate.php";
                Uri uri = new Uri(url, UriKind.Absolute);

                StringBuilder postData = new StringBuilder();
                postData.AppendFormat("{0}={1}", "username", HttpUtility.UrlEncode(this.username.Text));// txtUsername.Text));
                postData.AppendFormat("&{0}={1}", "password", HttpUtility.UrlEncode(this.password.Password.ToString()));
                try
                {
                    WebClient client = default(WebClient);
                    client = new WebClient();
                    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    client.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();


                    client.UploadStringCompleted += client_UploadStringCompleted;
                    //client.UploadProgressChanged += client_UploadProgressChanged;

                    client.UploadStringAsync(uri, "POST", postData.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Data.ToString());
                    MessageBox.Show(ex.GetBaseException().ToString());
                }

        }
            prog = new ProgressIndicator();
            prog.IsIndeterminate = true;
            prog.IsVisible = true;
            prog.Text = "Loading....";
            SystemTray.SetProgressIndicator(this, prog);


    }    

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试使用:

  catch(Webexception ex)
  {
     Messagebox.Show(ex.innerexception.tostring());
  }