This article is somewhat a part 2 of my Getting started with REST using Zend Framework article I wrote on Inchoo.net. My desire was to create a web service in ZendFramework than try to consume that web service in Mono framework using C# language. It’s more of a getting back on track with C# desire
When I started this little personal project I tried using SOAP as a web service. When it comes to ZendFramework, it’s really easy to create any of those two. However, C# gave me some headaches, or shall I say wsdl.exe was the reason I decided to put SOAP behind and do the REST.
Here is the working example of code used to consume REST service created in Getting started with REST using Zend Framework article.
// Main.cs created with MonoDevelop // User: branko at 10:23 AM 1/10/2009 // // To change standard headers go to Edit->Preferences->Coding->Standard Headers // using System; using System.Xml; using System.Web; using System.Net; using System.Web.Services; using System.IO; namespace ConsumeWebServiceFromZend { class MainClass { public static void Main(string[] args) { HttpWebRequest request; HttpWebResponse response; string uri = "http://localhost/codo/public/index.php/index/rest"; string method = "sayHello"; string methodParam = "Branko"; string restUri = uri + "?method=" + method + "&name=" + methodParam; Console.WriteLine(HttpGet(restUri)); Console.ReadLine(); } static string HttpGet(string url) { HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; string result = null; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string r = reader.ReadToEnd(); XmlDocument xd = new XmlDocument(); xd.LoadXml(r); XmlNodeList xn = xd.GetElementsByTagName("response"); result = xn[0].InnerText; return result; //return r; } } }
Hope someone finds it useful as a starting point in REST-full C#.
View Comments
Trackbacks/Pingbacks