views:

2205

answers:

2

How do I consume a RESTful web service in C# code?

NOTE: by RESTful, I mean non-SOAP. For example, in the flickr API you can call their web service as follows to return JSON data:

http://api.flickr.com/services/feeds/photos_public.gne?tags=cats&tagmode=any&format=json

Do I simply use HttpWebRequest?

What if I'm using a web service that takes POST parameters? Would that use HttpWebRequest also?

Is there any benefit to using WCF on the consumer side?

+3  A: 

WCF provides a unified programming model for communication, so if you later decide that you do not want to use REST or you want to provide an extra type of endpoint for example SOAP, you only need tp change the configuration.

Take a look at REST for WCF:

http://msdn.microsoft.com/en-us/netframework/cc950529.aspx

Shiraz Bhaiji
This documentation at this link suggests I use the HttpClient class from WCF REST starter kit, which is currently a preview release. I can't use a preview release in my environment -- should I just use HttpWebRequest instead?
frankadelic
Then you could use HttpWebRequest, this link may be helpfull http://stackoverflow.com/questions/1443858/reading-xml-from-aspx-web-page
Shiraz Bhaiji
HttpClient is built on top of HTTPWebRequest and actually has no dependency on WCF unless you use the Syndication stuff. The full source and it is well worth looking at. It is an excellent example of how to wrap HTTP WebRequest properly for use with REST services.
Darrel Miller
A: 

Hi Shiraz,

Their is a demonstration and labs in the .NET Framework 3.5 Enhancements Training Kit, which provides two labs based around creating, servicing and consuming RESTFul services. The above link is the best MS combined resources of REST services.

Bob.

scope_creep