【发布时间】: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