Monday, April 18, 2011

Consuming ReST Service

Today let us review how to consume the ReST service that was created in previous post.

Given below are 3 ways to consume the service


1) By using XMLDocument

XmlDocument oDoc = new XmlDocument();
oDoc.Load("http://localhost:49492/UserService.svc/users/World");
Console.WriteLine(oDoc.InnerXml);
Console.ReadLine();

2) Using Http Request


HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create("http://localhost:49492/UserService.svc/users/World");
HttpWebResponse webResponse = (HttpWebResponse)oRequest.GetResponse();
Stream receiveStream = webResponse.GetResponseStream();
StreamReader oReader = new StreamReader(receiveStream);
Console.WriteLine(oReader.ReadToEnd());
oReader.Close();
webResponse.Close();
Console.ReadLine();

3) Using Channel Factory


ChannelFactory factory = new ChannelFactory(new WebHttpBinding(), new EndpointAddress("http://localhost:49492/UserService.svc"));
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
IUserService proxy = factory.CreateChannel();
Console.WriteLine(proxy.GetFullName("World"));

int i = proxy.PutUserAccount("world", "newval");
Console.WriteLine(i.ToString());
Console.ReadLine();

No comments: