C# 隔离存储-异常的奇怪错误
我尝试将图片保存在设备上的隔离存储中。我关闭互联网并点击应用程序中的按钮。第一次,异常正确显示: “远程服务器返回了一个错误: 没有找到。” 下一步:我打开互联网并点击按钮-图像保存在我的手机上。 一切都好 现在最重要的是:当我再次关闭互联网时,我再也看不到这个异常(如果我想看到,我必须卸载,然后再次安装应用程序) 为什么? p、 s:对不起,我的英语不好C# 隔离存储-异常的奇怪错误,c#,windows-phone-7,exception,windows-phone-8,isolatedstorage,C#,Windows Phone 7,Exception,Windows Phone 8,Isolatedstorage,我尝试将图片保存在设备上的隔离存储中。我关闭互联网并点击应用程序中的按钮。第一次,异常正确显示: “远程服务器返回了一个错误: 没有找到。” 下一步:我打开互联网并点击按钮-图像保存在我的手机上。 一切都好 现在最重要的是:当我再次关闭互联网时,我再也看不到这个异常(如果我想看到,我必须卸载,然后再次安装应用程序) 为什么? p、 s:对不起,我的英语不好 public void update() { WebClient client = new WebClient(); cli
public void update()
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(@"http://www.myurl.com/" + path + "/" + number.ToString() + ".jpg"), client);
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
try
{
var resInfo = new StreamResourceInfo(e.Result, null);
var reader = new StreamReader(resInfo.Stream);
byte[] contents;
using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
{
contents = bReader.ReadBytes((int)reader.BaseStream.Length);
}
if (!MyStore.DirectoryExists(path))
MyStore.CreateDirectory(path);
IsolatedStorageFileStream stream = MyStore.CreateFile(path +"/"+ number.ToString() + ".jpg");
stream.Write(contents, 0, contents.Length);
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
您的图像在第一次请求后已被缓存,所有其他请求将首先尝试从缓存中获取该文件,如果已找到该文件,则不会向internet发出任何请求 如果要避免,请执行以下操作: 1) 在您自己的带有文件的服务器上,您可以将
缓存控制:max age
头值修改为可以缓存图像的某个时间
2) 为每个请求生成唯一的请求URI,如client.OpenReadAsync(新URI(@)http://www.myurl.com/“+path+”/“+number.ToString()+”.jpg“+”?rand=“+GetRandomNumber()),客户端)代码>它的作品:)。非常感谢您的解答和解释。