我将 Wamp 服务器用于后端,将 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);
}
请帮我。提前致谢。