【问题标题】:How to catch The remote server returned an error: NotFound?如何捕捉远程服务器返回错误:NotFound?
【发布时间】:2023-03-12 00:00:02
【问题描述】:

我的项目目标是 Windows Phone 7.5 及更高版本。

我使用一种方法获取在线图片并检查图片的类型,如果是gif则将其转换为jpg并将其绑定到图像控件,如果是jpg和png,则无需编码即可绑定。

但是下面的代码经常报错,“远程服务器返回错误:NotFound”,为什么?我已经捕获了 WebException。

public void GetOnlineImageAndReturnJPGStream(Action<Stream, string> callback, string uriString)
        {
            string errorstring = "";
            try
            {
                WebClient wc = new WebClient();
                wc.Headers[HttpRequestHeader.Referer] = "http://www.xici.net";
                wc.AllowReadStreamBuffering = true;
                wc.OpenReadCompleted += (s, e) =>
                {
                    if (e.Error == null && !e.Cancelled)
                    {
                        //check pic type
                        ImageTypeCheck.ImageType incomingIMGType = ImageTypeCheck.getImageType(e.Result);

                        switch (incomingIMGType)
                        {
                            case ImageTypeCheck.ImageType.Gif://if gif 
                                //deal with gif
                            case ImageTypeCheck.ImageType.Null:
                            case ImageTypeCheck.ImageType.Bmp:
                                //deal with bmp
                            case ImageTypeCheck.ImageType.Jpg:
                            case ImageTypeCheck.ImageType.Png:
                                //deal with jpg and png
                        }
                    }
                    else
                    {
                        errorstring = e.Error.Message;
                        callback(e.Result, errorstring);
                    }
                };

                wc.OpenReadAsync(new Uri(uriString, UriKind.Absolute));
            }
            catch (WebException webEx)
            {
                App.ShowToastNotification(webEx.Message);
            }
        }

未处理异常如下:

{System.Net.WebException:远程服务器返回错误: 未找到。 ---> System.Net.WebException:远程服务器返回一个 错误:未找到。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(Object 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b_0(Object sendState) --- 内部异常堆栈跟踪结束 --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() 在 System.Net.OpenReadCompletedEventArgs.get_Result() 在 xicihutong.DataServiceAgent.ServiceAgent.c_DisplayClassa.b_8(Object s, OpenReadCompletedEventArgs e) 在 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) 在 System.Net.WebClient.OpenReadOperationCompleted(对象 arg)} [System.Net.WebException]:{System.Net.WebException:远程服务器返回错误:NotFound。 ---> System.Net.WebException: 远程服务器返回错误:未找到。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(Object 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b_0(Object sendState) --- 内部异常堆栈跟踪结束 --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() 在 System.Net.OpenReadCompletedEventArgs.get_Result() 在 xicihutong.DataServiceAgent.ServiceAgent.c_DisplayClassa.b_8(Object s, OpenReadCompletedEventArgs e) 在 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) 在 System.Net.WebClient.OpenReadOperationCompleted(对象 arg)} _className:“System.Net.WebException” _数据:空 _dynamicMethods:空 _exceptionMethod:空 _exceptionMethodString:空 _helpURL:空 _H结果:-2146233079 innerException: {System.Net.WebException: 远程服务器返回错误:NotFound。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(Object 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b__0(Object 发送状态)} _ipForWatsonBuckets: 0 _message:“远程服务器返回错误:未找到。” _remoteStackIndex:0 _remoteStackTraceString:空 _来源:空 _stackTrace:{sbyte[96]} _stackTraceString:空 _watsonBuckets:{字节[5616]} _xcode:-532462766 xptrs: 0 数据:{System.Collections.ListDictionaryInternal} 帮助链接:空 H结果:-2146233079 InnerException:{System.Net.WebException:远程服务器返回错误:NotFound。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(Object 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b__0(Object 发送状态)} IPForWatsonBuckets: 0 消息:“远程服务器返回错误:NotFound。” 远程堆栈跟踪:空 来源:《系统》 StackTrace: " 在 System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()\r\n 在 System.Net.OpenReadCompletedEventArgs.get_Result()\r\n 在 xicihutong.DataServiceAgent.ServiceAgent.c_DisplayClassa.b_8(Object s, OpenReadCompletedEventArgs e)\r\n at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)\r\n 在 System.Net.WebClient.OpenReadOperationCompleted(Object arg)" WatsonBuckets:{byte[5616]}

为什么?以及如何处理?
不幸的是,我发布的错误消息是 Unhandle Exception 并告诉我我们的服务器返回了错误,但我认为我已经在 Unhandle 异常中捕获了 404 错误,为什么它仍然会抛出它?

【问题讨论】:

  • 你设置ID_CAP_NETWORKING 能力了吗?

标签: c# windows-phone-7 exception try-catch webclient


【解决方案1】:

要获取有关异常原因的更多详细信息,请检查WebException 对象的Status 属性。如果您的应用发送 https 请求,也可能是证书问题。

【讨论】:

  • 可惜我贴的错误信息是Unhandle Exception,告诉我我们的服务器返回错误,但是我以为我已经在Unhandle异常中捕获了404错误,为什么会抛出它还是?
【解决方案2】:

您无法以所述方式捕获异常,因为当您访问 Result 属性时,它会在 OpenReadCompleted 事件处理程序中异步引发。如果发生错误,您将无法获得Result,因此会引发异常。要处理它,请将 try catch 块放在事件处理程序中,但实际上为了防止异常,不要将 Result 传递给回调,以防发生错误。

【讨论】:

    【解决方案3】:

    调试它并尝试在浏览器中打开您正在下载的图像的完整地址。网址中可能缺少斜杠或其他内容。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多