【问题标题】:HttpClient.GetBufferAsync() does not compileHttpClient.GetBufferAsync() 无法编译
【发布时间】:2018-08-21 14:11:36
【问题描述】:

我有一个程序,我想在其中从 Internet 下载图像。我遵循了本教程: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();

【问题讨论】:

  • 听起来您缺少参考。你能检查一下你的项目是否引用了所需的程序集吗?
  • 您的应用程序实际上是 UWP 应用程序和HttpClient 那个类 而不是this one
  • 这是一个 uwp 应用程序@Damien_The_Unbeliever
  • 我在另一个工作正常的文件中使用 HttpClient。但我没有使用 GetBufferAsync() @Dimitar

标签: c# image download uwp


【解决方案1】:

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

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

【讨论】:

  • 两者有什么区别?
  • @Hemil - 目前对 很重要的两者之间最明显的区别是,一个有 GetBufferAsync 方法,而另一个没有:-|
猜你喜欢
  • 2015-11-25
  • 2015-06-07
  • 2013-03-06
  • 2016-08-24
  • 2018-10-18
  • 2014-02-16
  • 2015-03-31
  • 2015-10-29
  • 2013-01-29
相关资源
最近更新 更多