3

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.

Community
  • 1
  • 1
Tejas Sutar
  • 747
  • 2
  • 11
  • 33

2 Answers2

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