Consuming ASP.NET Web API Service using HttpClient: Part2 of Many

Read Part 1 Here

I am in love with ASP.NET Web API hence I started writing on the same. In first part of this blog series I have written Creating First HTTP Service using ASP.NET Web API: Part1 of Many.

In this post I will show you how you could consume service created in previous blog post in a managed application?

Service is hosted at http://localhost:14892/api/Bloggers

To download JSON data from service as string you can use WebClient class from System.Net as below. In below code essentially we are downloading JSON data as string from the service.


using System;
using System.Net;

namespace ConsoleApplication39
{
class Program
{
static void Main(string[] args)
{
using(WebClient proxy = new WebClient())
{
var response = proxy.DownloadString("http://localhost:14892/api/Bloggers");
Console.WriteLine(response);
Console.ReadKey(true);

}
}
}
}

As output you will get downloaded string on console

clip_image001

In above code while making call to service; we are not providing any header type and as response we are getting JSON since default serealizer of ASP.NET Web API is JSON serealizer. If you want to get XML data as response then you need to set Accept Header type to XML. Below code will download XML data from the service.


using System;
using System.Net;

namespace ConsoleApplication39
{
class Program
{
static void Main(string[] args)
{
using(WebClient proxy = new WebClient())
{
proxy.Headers.Add(HttpRequestHeader.Accept, "application/xml");
var response = proxy.DownloadString("http://localhost:14892/api/Bloggers");
Console.WriteLine(response);
Console.ReadKey(true);

}
}
}
}

On Console you will get expected XML as below,

clip_image002

Other than WebClient class, you can use HttpClient class. It is rich Http Client for .NET. It exposed the API to

  1. Receive HttpResponseMessage
  2. Send HttpRequestMessage

To work with HttpClient class you need to add below DLL in the project. You can download these dll from here

clip_image001[7]

You can download data as JSON array using HttpClient as below. We are downloading data asynchronously and using properties name printing them.


using System;
using System.Net.Http;
using System.Json;

namespace ConsoleApplication39
{
class Program
{
static void Main(string[] args)
{

HttpClient proxy = new HttpClient();
proxy.GetAsync("http://localhost:14892/api/Bloggers").ContinueWith((r) =>
{
HttpResponseMessage response = r.Result;
response.Content.ReadAsAsync<JsonArray>().ContinueWith(
(a)=>
{
foreach(var w in a.Result)
{
Console.WriteLine(w.ValueOrDefault("Name").ToString());
Console.WriteLine(w.ValueOrDefault("Intrest").ToString());
}
});

});

Console.ReadKey(true);

}

}
}

You will be getting output on console as below.

clip_image001[9]

Now question may come to your mind that why to use HttpClient over WebClient? You can use same instance of HttpClient to make as many call to service as you want. HttpClient allows you to configure headers extension etc.

In this way you can consume an Http Service created using ASP.Net Web API in a managed client. I hope this post is useful. Thanks for reading.

8 responses to “Consuming ASP.NET Web API Service using HttpClient: Part2 of Many”

  1. Terrence Spencer

    Great post, can you whip up a quick post on actual HttpClient CRUD calls to a webapi. Use some simple poco and do add update and delete.
    Thanks

  2. […] Consuming ASP.NET Web API Service using HttpClient: Part2 of Many (Dhananjay Kumar) […]

  3. Dhananjay Kumar

    sure will do

  4. Thanks for this post! Good Stuff.
    Any chance you could add a second entity class that has a relationship to the blogger class. I seem to be having difficulties in making the Get return the children. Seems that this should work. I’m sure I’m just doing something stupid. 🙂
    thanks!
    Bill44077

  5. […] Consuming ASP.NET Web API Service using HttpClient: Part2 of Many […]

  6. […] Creating First HTTP Service using ASP.NET Web API: Part1 of Many Consuming ASP.NET Web API Service using HttpClient: Part2 of Many […]

  7. […] Consuming ASP.NET Web API Service using HttpClient: Part2 of Many […]

  8. HI ,
    After getting response data instead of displaying console Store a File,
    How can i do please give me some code for this

Leave a comment

Create a website or blog at WordPress.com