Geeks With Blogs

News
Vitaly Dilmukhametov

WCF REST Starter Kit Preview 2 contains not only tools for creating REST services. It include rich set of client tools, that allow to do HTTP-requests and process service response in more convenient way. I create small overview of it basic features.

We’ll create a simple WPF desktop client for Yahoo Shopping Web Services. Our client sill search products by the keyword. Details of Search API is available here, but information about GET-request format and service URI is enough. For example, if we want to search for “digital camera”, we shall use following URL: http://shopping.yahooapis.com/ShoppingService/v3/productSearch?appid=YahooDemo&query=digital+camera&show_numratings=1. Parameter appId is a Yahoo id of your app. We can you demo id for our purposes and use “YahooDemo”. Keyword is passed through query parameter, and show_numratings=1 allow us to receive user rating of each product in the results. So, we need to do some GET-request and process server response.

User of our application enter search keywords to the TextBox, and we use Microsoft.Http.HttpQueryString class for making the URL:

string keyword = searchString.Text.ToLower().Trim().Replace(' ', '+');
 
HttpQueryString query = new HttpQueryString();
query.Add("appid", "YahooDemo");
query.Add("query", keyword);
query.Add("show_numratings", "1");

 

We simply add “parameter-value” pairs to the list, and call of MakeQueryString() method allow us to get the Uri:

string uri = query.MakeQueryString(new Uri("http://localhost/script.php")).AbsoluteUri;

 

In this example we receive following: http://localhost/script.php?appid=YahooDemo&query=notebook&show_numratings=1

We’ll use HttpClient class for sending request to the server. It allow us to execute wide range of HTTP-requests, it have multiple settings, for example HTTP headers, timeout of sending/receiving and other, it support sync and async requests, etc. And I think, it has more convenient API, then classic HttpWebRequest. And WebClient class don’t have some “must have” features, such as timeout settings (for example, if you try to POST large amount of data to the server through the poor channel, you often fail with timeout). Let’s take a look at HttpClient class:

HttpClient client = new HttpClient(@"http://shopping.yahooapis.com/ShoppingService/V3/");
client.DefaultHeaders.UserAgent.AddString(@"Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)");
HttpResponseMessage response = client.Get(new Uri("productSearch", UriKind.Relative), query);

 

With these 3 lines of code we perform sync GET-request to the server, and receive the response. We create instance of HttpClient with the base part of URL in the constructor. Then we add UserAgent header, because else server is not process the request normally. After that we make a request by calling Get() extension method. We can call REST service in other way:

HttpResponseMessage response = client.Send(HttpMethod.GET,query.MakeQueryString(new Uri("productSearch", UriKind.Relative)), HttpContent.CreateEmpty());

 

Here is used “universal” Send() method, where type of HTTP-method is typed explicitly. Of course, Get(), Post() and etc. is calling Send() internally, but usage of them is making code more “human-readable”. You can check state of response:

response.EnsureStatusIsSuccessful();

 

So, if HTTP-response code is not 2xx, then we get an exception. The value of response code is available through StatusCode property.

Ok, we get an XML response. But we need more not an XML but a collection of entities. You can create entity classes manually. Or you can route this task to the “Paste XML as Type” add-in for VS 2008. This add-in is included in the WCF REST Starter Kit. It have very simple UI – just a single item in the Edit menu of VS:

image8

You can copy XML response of the server and use this add-in. As a result you’ll get a set of generated entity classes, using it you can parse server response and get an instances. One disadvantage – sometime names of the types and properties is not useful and you need to change it manually.

So, you can get collection of the elements as following:

ProductSearch results = response.Content.ReadAsXmlSerializable<ProductSearch>();

 

That’s all. We only need to data-bind result of request and or client is ready:

image16

We can receive server response in other ways:

  • as byte[] by calling ReadAsByteArray();
  • as string by calling ReadAsString();
  • as stream by calling ReadAsStream();
  • as XElement for processing with XLINQ by calling ReadAsXElement();
  • as XmlReader by calling ReadAsXmlReader();

Ok, but that about services, which use JSON or RSS/Atom format? Starter Kit’s client part have appropriate extension-methods for HttpContent: ReadAsJsonDataContract<T>() and ReadAsSyndicationFeed().

With the source codes of WCF REST Starter Kit Preview 2 you can get an interesting WPF application, that allow to perform different types of HTTP-requests and analyze the response. You can see other “real world” examples of usage client parts on this sample app. And you must know, that I describe only basic features of client part of Starter Kit. It contains interesting and useful advanced possibilities.

Posted on Wednesday, October 28, 2009 1:05 PM WCF , REST , .NET | Back to top


Comments on this post: Client part of WCF REST Starter Kit

# re: Client part of WCF REST Starter Kit
Requesting Gravatar...
The WCF REST Starter Kit is a set of features, Visual Studio templates, samples and guidance that enable users to create REST style services using WCF. While the October release of the WCF REST Starter Kit focused on building server-side REST services
Left by alabama web design on Mar 12, 2010 6:57 PM

Your comment:
 (will show your gravatar)


Copyright © Vitus | Powered by: GeeksWithBlogs.net