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.

About these ads

7 thoughts on “Consuming ASP.NET Web API Service using HttpClient: Part2 of Many

  1. 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. Pingback: Dew Drop – March 8, 2012 (#1,282) | Alvin Ashcraft's Morning Dew

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

  4. Pingback: Monthly Report March 2012: Total Posts 15 « debug mode……

  5. Pingback: How to Self-Host ASP.Net Web API: Part 3 of Many « debug mode……

  6. Pingback: ASP.Net Web API Service in Windows Phone: Part 4 of Many « debug mode……

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s