I am using Microsoft.AspNet.WebApi.Client to consume rest services in my ASP.MVC 5 project. I am following the this tutorial to make use of HttpClient. The code is not compiling since ReadAsAsync
method is no longer available in HttpContent. After digging a bit I came to know that it is an extension method defined in System.Net.Http.Formatting.dll
. I found a nuget package for the same dll here but the package is deprecated and I am not able to install it. I also trid to search that dll in Program Files folder according to this post but I could not get it. Any ideas how to make ReadAsAsync
work? Any help greatly appreiciated. Thanks.
Asked
Active
Viewed 7,009 times
3

Community
- 1
- 1

Tejas Sutar
- 747
- 2
- 11
- 33
-
1Add reference and include `System.Net.Http;` and `System.Net.Http.Formating;` version 4.0.0.0 – Onel Sarmiento Oct 29 '14 at 07:08
-
Where can I get that dll from? Any idea? I tried installing it through Packge Manger Console but it says 'Unable to find version '4.0.0.0' of package 'System.Net.Http.Formatting'. It is deprecated I guess. – Tejas Sutar Oct 29 '14 at 07:12
-
2You already have `Http.Formatting` inside your GAC. – Yuval Itzchakov Oct 29 '14 at 07:16
-
2@TejasSutar Right Click Your Project>Add Reference>Assemblies> and include the `System.Net.Http` and `System.Net.Http.Formating` – Onel Sarmiento Oct 29 '14 at 07:20
-
Thanks for your efforts @Leonel..!! – Tejas Sutar Oct 29 '14 at 07:34
2 Answers
1
What do you need to do is to add new reference System.Net.HttpClient;
and System.Net.HttpClient.Formating;
.
This is my sample codes in HttpClient:
The following codes is use to get a certificate from saba using HttpClient.
using System.Net.Http;
using System.Net.Http.Headers;
using GoSaba.Models.Saba;
namespace GoSaba.Controllers.Saba
{
class LoginController
{
//HTTP GET: Saba/api/login
public async Task<string> GetCertificate(string host, string user, string password, string site)
{
StringBuilder getCertificate = new StringBuilder();
if(!string.IsNullOrEmpty(host))
{
using(var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(string.Format("http://{0}/", host));
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("user", user);
httpClient.DefaultRequestHeaders.Add("password", password);
httpClient.DefaultRequestHeaders.Add("site", site);
HttpResponseMessage httpResponse = await httpClient.GetAsync("Saba/api/login");
if(httpResponse.IsSuccessStatusCode)
{
LoginModel.GetCertificate saba = await httpResponse.Content.ReadAsAsync<LoginModel.GetCertificate>();//LoginModel.GetCertificate is model.
getCertificate.Append(saba.certificate);
}
}
}
return getCertificate.ToString();
}
}
}
You can use this a reference in how to use a HttpClient.

Onel Sarmiento
- 1,608
- 3
- 20
- 46
-
I don't have System.Net.Http.Formatting on my computer, which .Net version installs it? – user3285954 Mar 22 '16 at 18:36
-
It is on my work computer but not on my home computer. Both have .Net 4.6.1 installed. – user3285954 Mar 22 '16 at 18:48
0
Here is an alternate way using the same Microsoft.AspNet.WebApi.Client. The number of lines of code increase but you wont find any issue like Newtonsoft version issue, which prompted me to look at alternatives for ReadAsAsync.
Here is a link that explains the code - https://www.newtonsoft.com/json/help/html/Performance.htm
HttpClient client = new HttpClient();
using (Stream s = client.GetStreamAsync("http://www.test.com/large.json").Result)
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
JsonSerializer serializer = new JsonSerializer();
// read the json from a stream
// json size doesn't matter because only a small piece is read at a time from the HTTP request
T p = serializer.Deserialize<T>(reader); // Where T is any type
}

Varun
- 422
- 3
- 14