HttpClient.GetBufferAsync()无法编译

时间:2018-08-21 14:11:36

标签: c# image download uwp

我有一个程序,希望从互联网上下载图像。我遵循了本教程:How to download and store an image using Windows.Web.Http?

这是我的源代码:

Uri uri = new Uri(CoverImage);
string filename = BookNameTextBox.Text + ".jpg";

StorageFile destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename);

HttpClient client = new HttpClient();

var buffer = await client.GetBufferAsync(uri);

StorageFile photo = await ApplicationData.Current.LocalFolder.GetFileAsync(CoverImage);

程序无法编译。它说:“ HttpClient不包含'GetBufferAsync'的定义”。在谷歌上搜索时,我发现本文https://docs.microsoft.com/en-us/uwp/api/windows.web.http.httpclient.getbufferasync所依据的是该方法的存在。我不知道该怎么办。

具有讽刺意味的是,我实际上在另一个文件中使用了HttpClient,并且工作正常。这就是那部分:

var http = new HttpClient();

string url = "https://www.googleapis.com/books/v1/volumes?q=" + title.Replace(' ', '+') + "&fields = items(volumeInfo(title, authors, publisher)), items/";
var response = await http.GetAsync(url);
var result = await response.Content.ReadAsStringAsync();

1 个答案:

答案 0 :(得分:2)

确保使用Windows.Web.Http.HttpClient类(而不是System.Net.Http.HttpClient类):

Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient();