C# 如何捕获远程服务器返回的错误:NotFound?

C# 如何捕获远程服务器返回的错误:NotFound?,c#,windows-phone-7,exception,try-catch,webclient,C#,Windows Phone 7,Exception,Try Catch,Webclient,我的工作目标是Windows Phone 7.5及以上版本。 我使用一种方法获取在线图像并检查图像的类型,如果它是gif,那么我会将其转换为jpg并将其绑定到图像控件,如果是jpg和png,只需绑定而不进行编码。 但是下面的代码经常抛出错误,“远程服务器返回错误:NotFound”,为什么?我已经捕获了WebException。 public void GetOnlineImageAndReturnJPGStream(Action<Stream, string> callback,

我的工作目标是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);
            }
        }
public void GetOnlineImageAndReturnJPGStream(操作回调,字符串URI字符串)
{
字符串errorstring=“”;
尝试
{
WebClient wc=新的WebClient();
wc.Headers[HttpRequestHeader.Referer]=”http://www.xici.net";
wc.AllowReadStreamBuffering=true;
wc.OpenReadCompleted+=(s,e)=>
{
如果(e.Error==null&&!e.Cancelled)
{
//检查pic类型
ImageTypeCheck.ImageType incomingIMGType=ImageTypeCheck.getImageType(e.Result);
开关(输入IMG类型)
{
案例ImageTypeCheck.ImageType.Gif://if Gif
//处理gif
案例ImageTypeCheck.ImageType.Null:
案例ImageTypeCheck.ImageType.Bmp:
//处理bmp
案例ImageTypeCheck.ImageType.Jpg:
案例ImageTypeCheck.ImageType.Png:
//处理jpg和png
}
}
其他的
{
errorstring=e.Error.Message;
回调(如结果、错误字符串);
}
};
OpenReadAsync(新Uri(uriString,UriKind.Absolute));
}
捕获(WebException webEx)
{
App.ShowToastNotification(webEx.Message);
}
}
未处理的异常如下所示:

{System.Net.WebException:远程服务器返回错误: 找不到。-->System.Net.WebException:远程服务器返回了 错误:找不到。位于 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult 异步结果)在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(对象 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b_0(对象 sendState)---内部异常堆栈跟踪的结束----at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfEssential() 在System.Net.OpenReadCompletedEventArgs.get_Result()处 xicihuton.DataServiceAgent.ServiceAgent.c_DisplayClassa.b_8(对象 s、 OpenReadCompletedEventArgs)位于 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) 在System.Net.WebClient.OpenReadOperationCompleted(对象arg)} [System.Net.WebException]:{System.Net.WebException:远程服务器返回了一个错误:NotFound。-->System.Net.WebException:错误 远程服务器返回错误:NotFound.at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult 异步结果)在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(对象 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b_0(对象 sendState)---内部异常堆栈跟踪的结束----at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfEssential() 在System.Net.OpenReadCompletedEventArgs.get_Result()处 xicihuton.DataServiceAgent.ServiceAgent.c_DisplayClassa.b_8(对象 s、 OpenReadCompletedEventArgs)位于 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) 在System.Net.WebClient.OpenReadOperationCompleted(对象arg)} _类名:“System.Net.WebException” _数据:空 _dynamicMethods:null _例外方法:null _exceptionMethodString:null _帮助URL:null _HResult:-2146233079 innerException:{System.Net.WebException:远程服务器返回错误:NotFound.at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult 异步结果)在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(对象 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b_u0(对象 sendState)} _IPForWatsonBucket:0 _消息:“远程服务器返回错误:NotFound。” _remoteStackIndex:0 _remoteStackTraceString:空 _来源:空 _堆栈跟踪:{sbyte[96]} _stackTraceString:空 _WatsonBucket:{byte[5616]} _xcode:-532462766 xptrs:0 数据:{System.Collections.ListDictionaryInternal} 帮助链接:空 HResult:-2146233079 InnerException:{System.Net.WebException:远程服务器返回错误:NotFound.at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult 异步结果)在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClasse.b_d(对象 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass1.b_u0(对象 sendState)} IPForWatsonBucket:0 消息:“远程服务器返回错误:NotFound。” RemoteStackTrace:空 资料来源:“系统” StackTrace:“位于System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfEssential()\r\n 在System.Net.OpenReadCompletedEventArgs.get\u Result()上\r\n在 xicihuton.DataServiceAgent.ServiceAgent.c_DisplayClassa.b_8(对象 s、 OpenReadCompletedEventArgs\r\n位于 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) \r\n在System.Net.WebClient.OpenReadOperationCompleted(对象 arg)” WatsonBucket:{byte[5616]}

为什么?如何处理它?
不幸的是,埃罗