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.