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
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,
Other than WebClient class, you can use HttpClient class. It is rich Http Client for .NET. It exposed the API to
- Receive HttpResponseMessage
- Send HttpRequestMessage
To work with HttpClient class you need to add below DLL in the project. You can download these dll from here
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.
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.
Follow @debug_mode
Leave a Reply