locked
How do you access restful services using C#? RRS feed

  • Question

  • User608622201 posted

    Hi;

    I have a restful client that I created in C# using web api 2. I am trying to write a client in C# that I can access that restful service. I am using .net 4.5.1 and every example I have seen uses HttpContent::ReadAsync which no longer seems to be a method in HttpContent. I do see Readasync for String, byte and stream. I have been looking for example code for how to uses these methods but can't find anything. The Get methods I will be accessing are passing back an object or list of objects and I'm not sure how to deserialize from the httpcontent. Can anyone point me to an example? 

    I have seen this article "http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client". The problem is it uses the method ReadAsync which is no longer a member of HttpContent.

    Friday, June 12, 2015 7:29 PM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 12, 2015 11:37 PM
  • User-782957977 posted

    Please use HttpClient as given below

    Get call

     using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:58745");
                     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                     var response = client.GetAsync("api/values/1").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }
    

    Post call

    using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:58745/");
                     var response = client.PostAsJsonAsync("api/Values",model).Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 13, 2015 5:59 PM

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 12, 2015 11:37 PM
  • User-782957977 posted

    Please use HttpClient as given below

    Get call

     using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:58745");
                     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                     var response = client.GetAsync("api/values/1").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }
    

    Post call

    using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:58745/");
                     var response = client.PostAsJsonAsync("api/Values",model).Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 13, 2015 5:59 PM