A myriad of messaging processing options with HttpClient

A myriad of messaging processing options with HttpClient

The HttpClient class provides support for invoking the HTTP uniform interface (Get, Post, Put, Delete, Head, etc). And it provides support for processing messages using a variety of different representations.  This is made possible through the HttpResponseMessage and HttpContent classes, which provides numerous methods for reading the message body as different representations. Here's a list of the possibilities:

  • ReadAsByteArray
  • ReadAsDataContract
  • ReadAsJsonDataContract
  • ReadAsServiceDocument
  • ReadAsStream
  • ReadAsString
  • ReadAsSyndicationFeed
  • ReadAsXElement
  • ReadAsXmlReader
  • ReadAsXmlSerializable

And many of these methods have generic version that allow you to specify a serializable type. You'll need to ensure that you have the appropriate namespace using statement within the file or some of these extension methods may not show up in intellisense.

And between HttpContent and HttpContentExtensions, you also have a similar set of methods for creating request messages from a variety of different formats:

  • Create(Byte[])
  • Create(Stream)
  • Create(String)
  • Create (XElement)
  • CreateAtom10SyndicationFeed
  • CreateDataContract
  • CreateJsonDataContract
  • CreateRss20SyndicationFeed
  • CreateXmlSerializable

Check out this screencast for a complete example that shows how to use some of the methods listed above.

Screencast: HttpClient and query string/form input

Screencast: HttpClient and query string/form input

I recently published a new screencast video on HttpClient: query string and form input.  Check out this post for more background on these features and a brief code example.

httpclient-querystring-form-input-200

This screencast shows you how to manage query string and form input required by RESTful services on the Web by using some of the new features in the WCF REST Starter Kit Preview 2. The example shows how to send this type of input into the Twitter REST API.

Check out our growing collection of free .NET screencasts and videos.  You can subscribe to the Pluralsight feed to be notified when new screencasts are published.  In addition, check out our growing library of Pluralsight On-Demand! training courses.

HttpClient makes query strings & form input a breeze

HttpClient makes query strings & form input a breeze

The WCF REST Starter Kit Preview 2 provides several classes for managing traditional query string and form input to RESTful services:  HttpQueryString, HttpUrlEncodedForm, and HttpMimeMultipartForm.  These make passing data into RESTful services much easier.

Here's an example using HttpQueryString:

HttpClient http = new HttpClient("http://twitter.com/statuses/");
http.TransportSettings.Credentials =
    new NetworkCredential(username, password);
HttpResponseMessage resp = null;
// add query string variables
HttpQueryString vars = new HttpQueryString();
vars.Add("id", screenname);
vars.Add("count", count);

resp = http.Get(new Uri("user_timeline.xml", UriKind.Relative), vars);
resp.EnsureStatusIsSuccessful();
DisplayTwitterStatuses(resp.Content.ReadAsXElement());
And here's another example using HttpUrlEncodedForm:
HttpUrlEncodedForm form = new HttpUrlEncodedForm();
form.Add("status", status);

resp = http.Post("update.xml", form.CreateHttpContent());
resp.EnsureStatusIsSuccessful();

These classes take care of properly encoding the input values, and the HttpUrlEncodedForm sets the appropriate content-type header for a form submission (application/x-www-form-urlencoded). You can also use HttpMimeMultipartForm when you need to generate multi-part MIME messages (to simulate a file upload etc). Check out this screencast to see these new classes in action.

Why Service Virtualization Matters

Why Service Virtualization Matters

Lately I've been spending a lot of time thinking about service virtualization and why it matters. Along the way I've been doing some work for William Oellermann's crew within Microsoft Services — they are responsible for building the Managed Services Engine (MSE) that brings many service virtualization concepts to life today on the Windows platform.

If you're interested in learning more about service virtualization, check out my short executive whitepaper on the topic that was released at the SOA/BP conference earlier this year:

Why Service Virtualization Matters (download PDF)
Abstract: Service virtualization is an emerging trend in the SOA landscape that focuses on providing a common infrastructure for building and managing a complex service ecosystem. This paper discusses the concept of service virtualization along with a brief description of the technical solutions Microsoft currently provides today.

Microsoft Services just released a new version of the MSE on CodePlex (February 09 CTP), which comes with a new-and-improved management tool (built with WPF) that really helps you visualize what's going on within your SOA environment and several new virtualization concepts and features. This release also comes with improved documentation and videos that will help you figure out how to get started.

It's interesting to note that service virtualization concepts apply to both SOAP and REST services. In fact, this new version of the MSE makes it possible to virtualize REST resources that you can manage just like any other MSE endpoint. I'll be writing more about service virtualization + MSE in the months ahead.

Twin Cities Connected Systems User Group – March 19th, 2009

If you are in Minneapolis on Thursday March 19th please join us for the Twin Cities Connected Systems User Group Meeting.


The meeting takes place at 8000 Norman Center Drive, Bloomington, MN 55437 (the building directly east of the Microsoft office) from 6:00 to 7:30


Steve Cavanagh and Balaji Thiagarajan will be delivering a presentation titled ‘Business Rule Execution via Managed Stored Procedures: A Data-centric Approach’.

"Paste XML as Types" in REST Starter Kit

Cross-posted from the Silverlight Web Services Team Blog. 


Just a quick announcement here of a release that will be interesting to Silverlight developers who want to access REST services. The WCF REST Starter Kit Preview 2 is now out, go grab it at http://msdn.com/wcf/rest. The release gives you a polished install/uninstall experience, so don’t be afraid to try it on your box, it won’t muck it up like “preview” software so frequently does.


This release gives you one interesting client-side feature that you may have heard me or Eugene speak about: Paste XML as Types. It’s a VS menu item which helps you use XmlSerializer with REST services. Frequently these services use human-readable documentation to describe the XML shape, and it is difficult to hand-code a type to use with XmlSerializer, especially when the XML instance is complex. For example check out this sample XML response from the Yahoo BOSS API. With this new feature it takes one click to generate the type:



Another interesting feature in the release is HttpClient – a sort of specialized WebClient – which can be used to programmatically access REST services using an extensible model for sending HTTP requests and processing HTTP responses. The model enables you to complete common HTTP/REST development activities required to consume an existing service in a fraction of the time you normally spend. Some convenient time-savers include query string support (build URIs as name/value pairs) and serialization support (easily plug in types generated with Paste XML as Types to read the response).


Unfortunately in this release the starter kit only contains a .Net version of HttpClient, which will not compile in Silverlight. We are considering porting this prototype to Silverilght, and if you get a chance to try it on .Net, please let us know of any feedback you have.


Cheers,
Yavor Georgiev, Program Manager
Connected Framework Team


 

Screencast: Consuming REST Services with HttpClient

Screencast: Consuming REST Services with HttpClient

I recently published a new screencast video on Consuming REST Services with HttpClient.

httpclient-consuming-rest-services-200

This screencast shows you how to use the new HttpClient API (found in the WCF REST Starter Kit Preview 2) to build a Twitter client application against the Twitter REST API. It's a more complete example that shows how to use several of the status operations on the Twitter REST service including the public_timeline, the friends_timeline, the user_timeline, update, and destroy (which means GET, POST, and DELETE requests). This one provides a more moderately-paced introduction than the Twitter read/write in under 3 minutes intro. 😉

Check out our growing collection of free .NET screencasts and videos.  Subscribe to the Pluralsight feed to be notified when new screencasts are published.  Also, check out our growing library of online .NET training courses — see what you can learn with Pluralsight On-Demand!

Screencast: Getting Started with the WCF REST Starter Kit Preview 2

Screencast: Getting Started with the WCF REST Starter Kit Preview 2

I just posted a new screencast video on Getting Started with the WCF REST Starter Kit Preview 2.  This one shows how to download the starter kit from CodePlex, what it contains, and how to get started.

httpclient-getting-started-200

This screencast shows you how to get started with the new version of the WCF REST Starter Kit (Preview 2) that you can download now from CodePlex. It shows how to use the starter kit to create a simple REST service, along with a simple client to consume it.

Check out our growing collection of free .NET screencasts and videos.  Subscribe to the Pluralsight feed to be notified when new screencasts are published.  Also, check out our growing library of online .NET training courses — see what you can learn with Pluralsight On-Demand!