tags:

views:

2449

answers:

5

What's the best way to consume REST web services from .NET?

+1  A: 

Probably WCF; check out

http://hyperthink.net/blog/announcing-the-wcf-rest-starter-kit/

Brian
+1  A: 

Do you want to consume or publish. If you want to consume, such as making requests the best way to interact with it is to figure out the type it will comback as, usually JSON or XML. After you have your type you can use XmlDocument or JavaScriptSerializer to pull back the information and use it.

If you want to produce a REST interface then you probably want to use either MVC a REST View or WCF as @Brian said.

Nick Berardi
+6  A: 

A straight forward and easy approach would be to use WebClient which is in the System.Net namespace.

Pretty much all you need to do is pass in the Uri required with any parameters needed in the form of a query string and you should get back the response in the form of a string, be it json or xml. For example.

using System.Net; 

string param = "hello";

string url = String.Format("http://somedomain.com/samplerequest?greeting={0}",param);

WebClient serviceRequest = new WebClient();
string response = serviceRequest.DownloadString(new Uri(url));

Then, like Nick mentioned, you can use XmlDocument or JavaScriptSerializer to manipulate the results as needed. Anyway I suggest checking out the documentation on it to see if it meets your needs. http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

Shane Kenney
You'll need to use WebClient.UploadData to be able to specify the method you use. HttpWebRequest is a bit more flexible and possibly more suited for interacting with REST.
Richard
+2  A: 

I like this idea: http://activecodeline.com/consuming-rest-service-in-mono-with-csharp/

Instead using WebClient like Kenney, he use HttpWebRequest and HttpWebResponse and give a example of implementation with StreamReader and XmlDocument .

Luís Custódio