我有以下代码从我的 Windows 8 应用程序中的网络服务器获取动态图像。
private async Task httpFetcher()
{
HttpClient httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Get, "http://www.reignofcomputer.com/imgdump/sample.png");
HttpResponseMessage response = await httpClient.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead);
Uri imageUri;
BitmapImage image = null;
var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"sample.png", CreationCollisionOption.ReplaceExisting);
var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0));
writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
await writer.StoreAsync();
writer.DetachStream();
await fs.FlushAsync();
writer.Dispose();
if (Uri.TryCreate(imageFile.Path, UriKind.RelativeOrAbsolute, out imageUri))
{
image = new BitmapImage(imageUri);
}
image1.Source = image;
}
这在大多数情况下都有效,但由于某种原因,有时我会得到一个
UnauthorizedAccessException(0x80070005,E_ACCESSDENIED),HResult -2147024891。
因为错误是随机的并且它通常有效,所以我不确定问题出在哪里。
任何帮助,将不胜感激。