Dealing with REST Web Services


At the time of writing this post, .NET has no specific facility to work with REST services. Though, some services over the web provide information in REST format. For example, http://www.geonames.org (that provides gegraphic information) has a lot of free web services to get geographical information. The other day, I needed to use these services to let the end user choose his/her country and then state from two drop-down lists. So I had to figure out how to consume such services. In this post we will use the following service as an case-study:

http://ws.geonames.org/countryInfo?lang=it&country=DE

If you navigate to this link you will get the following result:

As is seen, the resulting XML is pretty straight forward.  With knowing that REST web services work with GET and POST methods, it is nearly nothing except working with xml!

For example, to query a REST service, like the one I mentioned above, we can use LINQ’s XML functionality.  The XElement class (declared in System.Linq.XML) provides a Load method that can retrieve an XML data from a url with GET  method:

XElement rootXml = XElement.Load(“http://ws.geonames.org/countryInfo?lang=it&country=DE”);

var Countries = from C in rootXml.Elements()
select new { Code = (string)C.Element(“countryCode”),
Name = (string)C.Element(“countryName”) };
foreach (var x in Countries)
Console.WriteLine(“{0}  {1}”, x.Code, x.Name);
Console.ReadKey();

After retrieveing the full xml, you can extract any kind of information you want. no matter how complex is it!

Adding or modifying information using POST method is a bit more complex. First we have to obtain a channel by which we may POST the information. Then we have to create an instance of data entity in XML format. Finally we have to POST data through the channel.

To get access to REST service we use a HttpWebRequest since it lets use the POST method and let us specify the type of information we are posting.

HttpWebRequest channel = (HttpWebRequest)WebRequest.Create(“http://ws.geonames.org/countryInfo”);
channel.Method = “POST”;
channel.ContentType = “text/xml”;

HttpWebRequest class provides a stream to which we can write information :

StreamWriter sw = new StreamWriter(channel.GetRequestStream());

Then we can create a concerete entity to post:

XElement country = new XElement(“country” ,

new XElement(“countryCode”, “AUS”),

new XElement(“countryName” , “Australia”),

….

);

Finally we just need to save this XElement object to the stream we obtained from HttpWebRequest:

country.Save(sw);

This post shows how to deal with REST web services, or any other resource that let us work it in a POST/GET manner. Thanks to Linq to XML working with such services is now too easy.

Advertisements

3 thoughts on “Dealing with REST Web Services

  1. Barry E says:

    Many thanks for the post. I was just what I was looking for to help migrate a legacy C# app from using WSE2 (yuck) to REST for communicating with a Rails app.

  2. Thanks a TON, I’ve been searching the web for hours trying to find this exact kind of tutorial and I really appreciate your contribution.

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