我的项目目标是 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:远程服务器返回错误:NotFound。---> System.Net.WebException:远程服务器返回错误:NotFound。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.<>c_ DisplayClasse.b _d(Object sendState) 在 System.Net.Browser.AsyncHelper.<>c_ DisplayClass1.b _0 (对象 sendState)--- 内部异常堆栈跟踪结束 --- System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at System.Net.OpenReadCompletedEventArgs.get_Result() at xicihutong.DataServiceAgent.ServiceAgent.<> c_DisplayClassa.b_8(Object s, OpenReadCompletedEventArgs e) at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) at System.Net.WebClient.OpenReadOperationCompleted(Object arg)} [System.Net.WebException]: {System.Net.WebException: 远程服务器返回错误:未找到。---> System.Net.WebException:远程服务器返回错误:NotFound。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.<>c_ DisplayClasse.b _d(Object sendState) 在 System.Net.Browser.AsyncHelper.<>c_ DisplayClass1.b_0(Object sendState) --- 内部异常堆栈跟踪结束 --- System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at System.Net.OpenReadCompletedEventArgs.get_Result() at xicihutong.DataServiceAgent.ServiceAgent.<> c_DisplayClassa.b _8(Object s, OpenReadCompletedEventArgs e) at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) at System.Net.WebClient.OpenReadOperationCompleted(Object arg)} _className: "System.Net.WebException" _data: null _dynamicMethods: null _exceptionMethod:空_exceptionMethodString:空_helpURL:空_HResult:-2146233079 innerException:{System.Net.WebException:远程服务器返回错误:NotFound。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.<>c _DisplayClasse.b_d (Object sendState) 在 System.Net.Browser.AsyncHelper.<>c _DisplayClass1.b__0( Object sendState)} _ipForWatsonBuckets: 0 _message: "远程服务器返回错误:NotFound。" _remoteStackIndex: 0 _remoteStackTraceString: null _source: null _stackTrace: {sbyte[96]} _stackTraceString: null _watsonBuckets: {byte[5616]} _xcode: -532462766 xptrs:0 数据:{System.Collections.ListDictionaryInternal} HelpLink:null HResult:-2146233079 InnerException:{System.Net.WebException:远程服务器返回错误:NotFound。在 System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.ClientHttpWebRequest.<>c _DisplayClasse.b_d (Object sendState) 在 System.Net.Browser.AsyncHelper.<>c_DisplayClass1.b__0(Object sendState)} IPForWatsonBuckets: 0 消息:“远程服务器返回错误:NotFound。” RemoteStackTrace: null 来源: "System" StackTrace: " at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()\r\n at System.Net.OpenReadCompletedEventArgs.get_Result()\r\n at xicihutong.DataServiceAgent.ServiceAgent.<>c_ DisplayClassa .b _8(Object s, OpenReadCompletedEventArgs e)\r\n 在 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)\r\n 在 System.Net.WebClient.OpenReadOperationCompleted(Object arg)" WatsonBuckets: {byte[5616] }
为什么?以及如何处理?
不幸的是,我发布的错误消息是 Unhandle Exception 并告诉我我们的服务器返回错误,但我认为我已经在 Unhandle 异常中捕获了 404 错误,为什么它仍然抛出它?