Receiving POST data in ASP.NET
- by grast
Hi, I want to use ASP for code generation in a C# desktop application.
To achieve this, I set up a simple host (derived from System.MarshalByRefObject) that processes a System.Web.Hosting.SimpleWorkerRequest via HttpRuntime.ProcessRequest. This processes the ASPX script specified by the incoming request (using System.Net.HttpListener to wait for requests).
The client-part is represented by a System.ComponentModel.BackgroundWorker that builds the System.Net.HttpWebRequest and receives the response from the server.
A simplified version of my client-part-code looks like this:
private void SendRequest(object sender, DoWorkEventArgs e)
{
// create request with GET parameter
var uri = "http://localhost:9876/test.aspx?getTest=321";
var request = (HttpWebRequest)WebRequest.Create(uri);
// append POST parameter
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var postData = Encoding.Default.GetBytes("postTest=654");
var postDataStream = request.GetRequestStream();
postDataStream.Write(postData, 0, postData.Length);
// send request, wait for response and store/print content
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
_processsedContent = reader.ReadToEnd();
Debug.Print(_processsedContent);
}
}
}
My server-part-code looks like this (without exception-handling etc.):
public void ProcessRequests()
{
// HttpListener at http://localhost:9876/
var listener = SetupListener();
// SimpleHost created by ApplicationHost.CreateApplicationHost
var host = SetupHost();
while (_running)
{
var context = listener.GetContext();
using (var writer = new StreamWriter(context.Response.OutputStream))
{
// process ASP script and send response back to client
host.ProcessRequest(GetPage(context), GetQuery(context), writer);
}
context.Response.Close();
}
}
So far all this works fine as long as I just use GET parameters. But when it comes to receiving POST data in my ASPX script I run into trouble. For testing I use the following script:
// GET parameters are working:
var getTest = Request.QueryString["getTest"];
Response.Write("getTest: " + getTest); // prints "getTest: 321"
// don't know how to access POST parameters:
var postTest1 = Request.Form["postTest"]; // Request.Form is empty?!
Response.Write("postTest1: " + postTest1); // so this prints "postTest1: "
var postTest2 = Request.Params["postTest"]; // Request.Params is empty?!
Response.Write("postTest2: " + postTest2); // so this prints "postTest2: "
It seems that the System.Web.HttpRequest object I'm dealing with in ASP does not contain any information about my POST parameter "postTest". I inspected it in debug mode and none of the members did contain neither the parameter-name "postTest" nor the parameter-value "654". I also tried the BinaryRead method of Request, but unfortunately it is empty. This corresponds to Request.InputStream==null and Request.ContentLength==0. And to make things really confusing the Request.HttpMethod member is set to "GET"?!
To isolate the problem I tested the code by using a PHP script instead of the ASPX script. This is very simple:
print_r($_GET); // prints all GET variables
print_r($_POST); // prints all POST variables
And the result is:
Array
(
[getTest] = 321
)
Array
(
[postTest] = 654
)
So with the PHP script it works, I can access the POST data. Why does the ASPX script don't? What am I doing wrong? Is there a special accessor or method in the Response object?
Can anyone give a hint or even know how to solve this? Thanks in advance.