need help in rest services urgent plzhttp://forums.asp.net/t/1774390.aspx/1?need+help+in+rest+services+urgent+plzThu, 01 Mar 2012 05:40:46 -050017743904853548http://forums.asp.net/p/1774390/4853548.aspx/1?need+help+in+rest+services+urgent+plzneed help in rest services urgent plz <p>&nbsp;hi all,</p> <p>iam trying to implement rest services. i do not use WCF for now.</p> <p>have a webservice like this ---- mathServie.asmx</p> <p>[ <span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">WebMethod</span></span><span size="2" style="font-size:small">]</span></p> <p>&nbsp;&nbsp;&nbsp;<span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">public</span></span><span size="2" style="font-size:small"> </span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">int</span></span><span size="2" style="font-size:small"> addition(</span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">int</span></span><span size="2" style="font-size:small"> num1, </span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">int</span></span><span size="2" style="font-size:small"> num2)</span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;<span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">eturn</span></span><span size="2" style="font-size:small"> (num1 &#43; num2);&nbsp;</span> }</p> <p>&nbsp;</p> <p>calling from the .cs page:</p> <p><br> <span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">protected</span></span><span size="2" style="font-size:small"> </span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">void</span></span><span size="2" style="font-size:small"> Button1_Click(</span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">object</span></span><span size="2" style="font-size:small"> sender, </span><span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">EventArgs</span></span><span size="2" style="font-size:small"> e)</span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p><span size="2" color="#008000" style="color:#008000; font-size:small"><span size="2" color="#008000" style="color:#008000; font-size:small">//create a request&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p> <p>&nbsp;&nbsp;<span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">HttpWebRequest</span></span><span size="2" style="font-size:small"> request = </span><span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">WebRequest</span></span><span size="2" style="font-size:small">.Create(</span><span size="2" color="#a31515" style="color:#a31515; font-size:small"><span size="2" color="#a31515" style="color:#a31515; font-size:small">&quot;http://localhost:51716/WebServices/MyMathService.asmx/addition&quot;</span></span><span size="2" style="font-size:small">) </span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">as</span></span><span size="2" style="font-size:small"> </span><span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">HttpWebRequest</span></span><span size="2" style="font-size:small">;</span></p> <p>&nbsp;&nbsp;<span size="2" color="#008000" style="color:#008000; font-size:small"><span size="2" color="#008000" style="color:#008000; font-size:small">//make a string builder with params we need to send&nbsp; as i do not know&nbsp; how to send params in url</span></span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">StringBuilder</span></span><span size="2" style="font-size:small"> sbParams = </span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">new</span></span><span size="2" style="font-size:small"> </span><span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">StringBuilder</span></span><span size="2" style="font-size:small">();</span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sbParams.Append(<span size="2" color="#a31515" style="color:#a31515; font-size:small"><span size="2" color="#a31515" style="color:#a31515; font-size:small">&quot;12&quot;</span></span><span size="2" style="font-size:small">);</span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sbParams.Append(<span size="2" color="#a31515" style="color:#a31515; font-size:small"><span size="2" color="#a31515" style="color:#a31515; font-size:small">&quot;5&quot;</span></span><span size="2" style="font-size:small">);</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p><span size="2" color="#008000" style="color:#008000; font-size:small"><span size="2" color="#008000" style="color:#008000; font-size:small"></span></span>&nbsp;</p> <p><span size="2" color="#008000" style="color:#008000; font-size:small"><span size="2" color="#008000" style="color:#008000; font-size:small">//Encode the parameters as form data:</span></span></p> <p>&nbsp;&nbsp;<span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">byte</span></span><span size="2" style="font-size:small">[] dataToSend = </span><span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">UTF8Encoding</span></span><span size="2" style="font-size:small">.UTF8.GetBytes(sbParams.ToString());</span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.ContentLength = dataToSend.Length;</p> <p>// Pick up the response:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">using</span></span><span size="2" style="font-size:small"> (</span><span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">HttpWebResponse</span></span><span size="2" style="font-size:small"> resp = request.GetResponse() </span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">as</span></span><span size="2" style="font-size:small"> </span><span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">HttpWebResponse</span></span><span size="2" style="font-size:small">)</span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;<span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">StreamReader</span></span><span size="2" style="font-size:small"> reader =&nbsp; </span><span size="2" color="#0000ff" style="color:#0000ff; font-size:small"><span size="2" color="#0000ff" style="color:#0000ff; font-size:small">new</span></span><span size="2" style="font-size:small"> </span><span size="2" color="#2b91af" style="color:#2b91af; font-size:small"><span size="2" color="#2b91af" style="color:#2b91af; font-size:small">StreamReader</span></span><span size="2" style="font-size:small">(resp.GetResponseStream());</span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Label3.Text = reader.ReadToEnd();</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;</p> <p>i know my code is wrong coz it just getting stream but not invoking anuthing. i guess i need to use xmlreader instead of stream reader.</p> <p>or iam wrong that rest services showld be writen differnt way , not the regular webservice way.</p> <p>&nbsp;</p> <p>plz provide the correct code for above example to work. no WCF plz</p> 2012-02-27T19:05:51-05:004853599http://forums.asp.net/p/1774390/4853599.aspx/1?Re+need+help+in+rest+services+urgent+plzRe: need help in rest services urgent plz <p>For quick find out try to use fiddler and check what is the response you are getting back.</p> <p></p> 2012-02-27T19:43:33-05:004858067http://forums.asp.net/p/1774390/4858067.aspx/1?Re+need+help+in+rest+services+urgent+plzRe: need help in rest services urgent plz <p>Hi Anu_sha,<br> <br> Seems you have not supplied the data to send in the HTTP POST request. You need to first get the RequestStream from HttpWebRequest, write in your data to send. And then, Get the response from HttpWebRequest and read out the response data from it. Here is a simple example for your reference:</p> <p>#service method</p> <pre class="prettyprint">[WebMethod] public int Add(int lv, int rv) { return lv &#43; rv; }</pre> <p>&nbsp;</p> <p>#console client(use webrequest)</p> <pre class="prettyprint">private static void CallSimpleWebServiceWithHTTPPOST() { var request = (HttpWebRequest)WebRequest.Create("http://localhost:52452/TestWebService.asmx/Add"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; using (var sw = new StreamWriter(request.GetRequestStream())) { sw.Write( string.Format("lv={0}&amp;rv={1}", 3, 5 ) ); } var response = (HttpWebResponse)request.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) { Console.WriteLine(sr.ReadToEnd()); } }</pre> <p>&nbsp;</p> <p>BTw, I strongly recommend that you start using WCF to build REST service instead of the ASMX webservice if possible :)<br> <br> </p> <p>#WCF Web HTTP Programming Model Overview <a href="http://msdn.microsoft.com/en-us/library/bb412172.aspx"> h<br> ttp://msdn.microsoft.com/en-us/library/bb412172.aspx</a></p> <p>#WCF REST Service Template 40(CS) <br> <a href="http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd">http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd</a></p> <p>&nbsp;</p> <p>#Consuming WCF 4.0 REST Service in Windows Phone 7 <br> <a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=501">http://www.dotnetcurry.com/ShowArticle.aspx?ID=501</a></p> <p>#WCF Rest Services for Windows Phone <br> <a href="http://ganshani.com/2011/08/14/wcf-rest-services-for-windows-phone-2/">http://ganshani.com/2011/08/14/wcf-rest-services-for-windows-phone-2/</a></p> 2012-03-01T05:40:46-05:00