Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is HttpContent.ReadAsAsync?

I see in tons of examples on the web using the new HttpClient object (as part of the new Web API) that there should be HttpContent.ReadAsAsync<T> method. However, MSDN doesn't mention this method, nor does IntelliSense find it.

Where did it go, and how do I work around it?

like image 430
David Pfeffer Avatar asked May 01 '12 14:05

David Pfeffer


2 Answers

It looks like it is an extension method (in System.Net.Http.Formatting):

HttpContentExtensions Class

Update:

PM> install-package Microsoft.AspNet.WebApi.Client

According to the System.Net.Http.Formatting NuGet package page, the System.Net.Http.Formatting package is now legacy and can instead be found in the Microsoft.AspNet.WebApi.Client package available on NuGet here.

like image 74
J... Avatar answered Sep 20 '22 00:09

J...


I have the same problem, so I simply get JSON string and deserialize to my class:

HttpResponseMessage response = await client.GetAsync("Products"); //get data as Json string  string data = await response.Content.ReadAsStringAsync(); //use JavaScriptSerializer from System.Web.Script.Serialization JavaScriptSerializer JSserializer = new JavaScriptSerializer(); //deserialize to your class products = JSserializer.Deserialize<List<Product>>(data); 
like image 42
rosta Avatar answered Sep 17 '22 00:09

rosta