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:
#service method
[WebMethod] public int Add(int lv, int rv) { return lv + rv; }
#console client(use webrequest)
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}&rv={1}",
3,
5
)
);
}
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(sr.ReadToEnd());
}
}
BTw, I strongly recommend that you start using WCF to build REST service instead of the ASMX webservice if possible :)
Anu_sha
Member
85 Points
36 Posts
need help in rest services urgent plz
Feb 27, 2012 07:05 PM|LINK
hi all,
iam trying to implement rest services. i do not use WCF for now.
have a webservice like this ---- mathServie.asmx
[ WebMethod]
public int addition(int num1, int num2)
{ eturn (num1 + num2); }
calling from the .cs page:
protected void Button1_Click(object sender, EventArgs e)
{
//create a request
HttpWebRequest request = WebRequest.Create("http://localhost:51716/WebServices/MyMathService.asmx/addition") as HttpWebRequest;
//make a string builder with params we need to send as i do not know how to send params in url
StringBuilder sbParams = new StringBuilder();
sbParams.Append("12");
sbParams.Append("5");
//Encode the parameters as form data:
byte[] dataToSend = UTF8Encoding.UTF8.GetBytes(sbParams.ToString());
request.ContentLength = dataToSend.Length;
// Pick up the response:
using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
{ StreamReader reader = new StreamReader(resp.GetResponseStream());
Label3.Text = reader.ReadToEnd();
}
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.
or iam wrong that rest services showld be writen differnt way , not the regular webservice way.
plz provide the correct code for above example to work. no WCF plz
kushalrdalal
Contributor
7200 Points
1288 Posts
Re: need help in rest services urgent plz
Feb 27, 2012 07:43 PM|LINK
For quick find out try to use fiddler and check what is the response you are getting back.
My Blog
LinkedIn Profile
Steven Cheng...
Contributor
4219 Points
548 Posts
Microsoft
Moderator
Re: need help in rest services urgent plz
Mar 01, 2012 05:40 AM|LINK
Hi Anu_sha,
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:
#service method
[WebMethod] public int Add(int lv, int rv) { return lv + rv; }#console client(use webrequest)
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}&rv={1}", 3, 5 ) ); } var response = (HttpWebResponse)request.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) { Console.WriteLine(sr.ReadToEnd()); } }BTw, I strongly recommend that you start using WCF to build REST service instead of the ASMX webservice if possible :)
#WCF Web HTTP Programming Model Overview h
ttp://msdn.microsoft.com/en-us/library/bb412172.aspx
#WCF REST Service Template 40(CS)
http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd
#Consuming WCF 4.0 REST Service in Windows Phone 7
http://www.dotnetcurry.com/ShowArticle.aspx?ID=501
#WCF Rest Services for Windows Phone
http://ganshani.com/2011/08/14/wcf-rest-services-for-windows-phone-2/
Feedback to us
Microsoft One Code Framework