WCF REST Starter Kit Consumer quick example

Posted on: Fri Mar 05 16:13:29 -0800 2010. Updated on: Fri Mar 05 16:19:56 -0800 2010.
Category: DotNet

Here I will summarize some quick points about using the WCF Rest Starter Kit P2 to consume REST services. If you are not familiar with it, here is a good article from msdn: http://msdn.microsoft.com/en-us/library/ee391967.aspx

// quick code example .net accessing shopify api
private orders InitialOrderSync(string appURL, string apiKey, string pwd)
		{
// note here we can use http or https
			HttpClient client = new HttpClient("https://" + appURL);
// set optional credentials
			client.TransportSettings.Credentials = new NetworkCredential(apiKey, pwd);
			// the uri of the REST path to get, can also pass in parameters 
			HttpResponseMessage message = client.Get("admin/orders.xml");
			message.EnsureStatusIsSuccessful(); // make sure all ok
                        // serialize into object
			orders shopifyOrders = message.Content.ReadAsXmlSerializable();
			return shopifyOrders;
		}

You may be wondering how did I build the orders object based on the xml. This is very easy. Just copy the xml you will be serializing from start to finish, go into Visual Studio that has wcf rest kit installed, add a new Class, and go menu Edit -> Paste as XML Types. That's it.

As far as using requirements, add references to your project to all 3 binaries in the WCF REST kit installation directory folder.
in your code you'll need these guys for above to work:

using System.Xml.Serialization; using System.Xml; using System.Runtime.Serialization; using Microsoft.Http; using Microsoft.ServiceModel; using System.Net;

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.