C# System.Windows.ni.dll 中出现“System.Net.WebException”类型的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19904334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:05:15  来源:igfitidea点击:

An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll

c#.netwindows-phone-8-emulator

提问by faroke moahmed

I'm developing a Windows Phone 8 application. I am getting this error when I try to run my app:

我正在开发 Windows Phone 8 应用程序。当我尝试运行我的应用程序时出现此错误:

My error description

我的错误描述

An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll but was not handled in user code.

System.Windows.ni.dll 中出现“System.Net.WebException”类型的异常,但未在用户代码中处理。

My code

我的代码

private async void buttonStringGet_Click_1(object sender, RoutedEventArgs e)
{
    JsonWebAsync.JsonWebClient client = new JsonWebAsync.JsonWebClient();
    var resp = await client.DoRequestAsync("myurl");
    string result = resp.ReadToEnd();
    resultText.Text = result;
}

采纳答案by chwarr

For whatever reason, it looks like the remote web server is responding with 404 Not Found. Maybe the server is doing the right thing. If so, you're application is going to need to deal with this response in a sensible way. Maybe it isn't doing the right thing, and you have a bug in your server component to fix. :-)

无论出于何种原因,看起来远程 Web 服务器正在响应 404 Not Found。也许服务器正在做正确的事情。如果是这样,您的应用程序将需要以合理的方式处理此响应。也许它没有做正确的事情,并且您的服务器组件中有一个错误需要修复。:-)

I'd approach solving the application crash in two different ways.

我会以两种不同的方式解决应用程序崩溃。

First, let's handle this error so the application doesn't crash. As an example, we'll just populate the resultTextcontrol with some details about the error. As we have no details about what the request is supposed to do or what the response looks like, there isn't much more robust error handling that can be discussed right now. Keep in mind that networks calls don't always work, so you're going to need some error handling even if you address the second point, below.

首先,让我们处理这个错误,以免应用程序崩溃。作为示例,我们将仅resultText使用有关错误的一些详细信息来填充控件。由于我们没有关于请求应该做什么或响应是什么样的细节,现在没有可以讨论的更强大的错误处理。请记住,网络调用并不总是有效,因此即使您解决了下面的第二点,您也需要进行一些错误处理。

private async void buttonStringGet_Click_1(object sender, RoutedEventArgs e)
{
    JsonWebAsync.JsonWebClient client = new JsonWebAsync.JsonWebClient();

    string result;

    try
    {
        var resp = await client.DoRequestAsync("myurl");
        result = resp.ReadToEnd();
    }
    catch (WebException ex)
    {
         // generic error handling
         result = string.Format("Could not get data. {0}", ex);
    }

    resultText.Text = result;
}

Second, let's try to avoid the error in the first place. Let's make sure the request the client is sending makes sense. You could add some instrumentation code to make sure that whatever gets used instead of "myurl" is sensible.

其次,让我们首先尝试避免错误。让我们确保客户端发送的请求有意义。您可以添加一些检测代码以确保使用的任何内容而不是“myurl”都是合理的。

private async void buttonStringGet_Click_1(object sender, RoutedEventArgs e)
{
    JsonWebAsync.JsonWebClient client = new JsonWebAsync.JsonWebClient();

    string requestUrl = ComputeRequestUrl(); // I assume this code exists somewhere.
    System.Diagnostics.Debug.WriteLine("Sending request for {0}", requestUrl);
    var resp = await client.DoRequestAsync(requestUrl);
    string result = resp.ReadToEnd();
    resultText.Text = result;
}

Now, when you run a debug build with a debugger attached, you should be able to see the tracing output in the Output window of Visual Studio when you pick the Debug stream.

现在,当您运行带有调试器的调试版本时,当您选择调试流时,您应该能够在 Visual Studio 的输出窗口中看到跟踪输出。