tags:

views:

686

answers:

3

Greetings,

I'm confused as to the best approach to make when consuming REST based web services with .Net. At the moment I'm using the System.net.webclient class. Should I be using Webresponse, webrequest classes in System.Net ?

If I were to use another approach (Other than webclient) what disadvantages / advantages would I gain ?

Thanks,

+2  A: 

If you use the WCF REST starter kit, there's pretty much no technical downside. There's a learning curve, however, and it will only work if you have .Net 3.5 SP1 (so I guess that's the down side).

Michael Meadows
+1  A: 

You can also see this link if you want to do it without WCF.

Senad Uka
A: 

i found this post hope be useful for u

http://blog.flair-systems.com/2010/05/how-to-consume-rest-based-services.html

Q. How to consume REST based-services?

Assume we have REST based-service contains method Add which takes two operands op1 and op2 and return the summation.

A) Create any project type (Console, Windows or even Web-based application) and add the below lines :

Code Snippet

  1. using System.Net;
  2. using System.IO;

B) Write some code to consume the service:

Code Snippet

  1. string parameters;
  2. string response;
  3. // Create the request obj
  4. WebRequest request = WebRequest.Create("serviceURL/Add");
  5. // Set values for the request back
  6. request.Method = "POST"; //REST based-services using Post method
  7. request.ContentType = "application/json"; //tells request the content typs is JSON
  8. parameters = "{\"op1\":2,\"op2\":\"1\"}";
  9. request.ContentLength = parameters.Length;
    1. // Write the request
    2. StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
    3. requestWriter.Write(parameters);
    4. requestWriter.Close();
    5. // Do the request to get the response
    6. StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
    7. response = responseReader.ReadToEnd();
    8. responseReader.Close();

Response value should be 3 if you implement Add method correctly :))

Waleed Mohamed