1

I have a program in which I want to download an image from internet. I followed this tutorial: How to download and store an image using Windows.Web.Http?.

This was my source code:

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);

The program does not compile. It says, "HttpClient does not contain a definition for 'GetBufferAsync' ". While googling, I found this article https://learn.microsoft.com/en-us/uwp/api/windows.web.http.httpclient.getbufferasync according to which the method exists. I don't know what to do.

The irony is I actually use HttpClient in another file and it works fine. This is that part:

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();
Hemil
  • 916
  • 9
  • 27
  • It sound like you are missing a reference. Can you check if you project is referencing the needed assembly? – Dimitar Aug 21 '18 at 14:14
  • And is your application actually a UWP application and `HttpClient` *that class* rather than [this one](https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx)? – Damien_The_Unbeliever Aug 21 '18 at 14:14
  • It is a uwp app @Damien_The_Unbeliever – Hemil Aug 21 '18 at 14:25
  • I use HttpClient in one other file where it works fine. But I didn't use GetBufferAsync() there @Dimitar – Hemil Aug 21 '18 at 14:27

1 Answers1

2

Make sure that you use the Windows.Web.Http.HttpClient class (and not the System.Net.Http.HttpClient class):

Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient();
mm8
  • 163,881
  • 10
  • 57
  • 88
  • What is the difference between the two? – Hemil Aug 21 '18 at 14:30
  • @Hemil: https://stackoverflow.com/questions/31291008/system-net-http-httpclient-vs-windows-web-http-httpclient-what-are-the-main-di – mm8 Aug 21 '18 at 14:32
  • 1
    @Hemil - the most obvious difference between the two that matter to *you* currently is that one has a `GetBufferAsync` method and the other one doesn't :-| – Damien_The_Unbeliever Aug 21 '18 at 14:42