I m writing desktop frontend to ASP.NET web application. I do not want using web service for hosting portability reasons. I want to send data to application through POST request instead.
On client side i write:
public static string PostData(string sURL, string sData)
{
string result = "";
//string sData = "x=1&y=2&z=Ok";
try
{
// prepare request
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(sURL);
wreq.Method = "POST";
wreq.ContentType = "application/x-www-form-urlencoded";
wreq.ContentLength = sData.Length;
// post to server
Stream sw = wreq.GetRequestStream();
StreamWriter wr = new StreamWriter(sw);
wr.Write(wr);
wr.Close();
// get response
HttpWebResponse httpResponse = (HttpWebResponse)wreq.GetResponse();
Stream sr = httpResponse.GetResponseStream();
StreamReader rd = new StreamReader(sr);
result = rd.ReadToEnd();
}
catch (System.Exception ex) { return ex.Message.ToString(); }
return result;
}
On server side i write:
public class Test : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string result = "Error";
if (context.Request.RequestType == "POST")
{
string x = context.Request.Form["x"];
result = "OK";
}
context.Response.ContentType = "text/plain";
context.Response.Write(result);
}
public bool IsReusable { get { return false; } }
}
When I run this code, I get error on wr.Close();
"The request was aborted: The request was canceled."
May somebody advise on how this can be implemented correctly. I m sorry, if the question is elementary. I m just novice in ASP. Thanks.
bzinchenko
Member
9 Points
15 Posts
POST from C# to ASP.NET handler
Dec 22, 2009 06:59 AM|LINK
Hello everybody
I m writing desktop frontend to ASP.NET web application. I do not want using web service for hosting portability reasons. I want to send data to application through POST request instead.
On client side i write:
public static string PostData(string sURL, string sData) { string result = ""; //string sData = "x=1&y=2&z=Ok"; try { // prepare request HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(sURL); wreq.Method = "POST"; wreq.ContentType = "application/x-www-form-urlencoded"; wreq.ContentLength = sData.Length; // post to server Stream sw = wreq.GetRequestStream(); StreamWriter wr = new StreamWriter(sw); wr.Write(wr); wr.Close(); // get response HttpWebResponse httpResponse = (HttpWebResponse)wreq.GetResponse(); Stream sr = httpResponse.GetResponseStream(); StreamReader rd = new StreamReader(sr); result = rd.ReadToEnd(); } catch (System.Exception ex) { return ex.Message.ToString(); } return result; }On server side i write:
public class Test : IHttpHandler { public void ProcessRequest(HttpContext context) { string result = "Error"; if (context.Request.RequestType == "POST") { string x = context.Request.Form["x"]; result = "OK"; } context.Response.ContentType = "text/plain"; context.Response.Write(result); } public bool IsReusable { get { return false; } } }When I run this code, I get error on wr.Close();
"The request was aborted: The request was canceled."
May somebody advise on how this can be implemented correctly. I m sorry, if the question is elementary. I m just novice in ASP. Thanks.
Aleksey S Ne...
Member
96 Points
22 Posts
Re: POST from C# to ASP.NET handler
Dec 22, 2009 07:56 AM|LINK
maybe: wr.Write(sData)
bzinchenko
Member
9 Points
15 Posts
Re: POST from C# to ASP.NET handler
Dec 22, 2009 08:07 AM|LINK
Thanks a lot! Really silly misprint.