Receiving POST data in ASP.NET

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();

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);


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:

    [getTest] => 321
    [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.


This might be an encoding issue which results in your request stream being discarded. You should use:


as you will probably be sending the request using "Windows-1252" codepage.

Remove cookieless="true" from Web.config.

Today I had a similar problem. On my configuration, it was caused by a URL-REWRITING rule. The rule was redirecting all pages from HTTP to HTTPS and when redirecting, the POST data were lost. So for me, posting data to HTTPS instead of HTTP had helped (since the rule did not apply then).

Need Your Help

load external files using ajax/query

html ajax forms jquery

I'm having 2 buttons to load 2 separate forms, events and offers. when a user clicks on a button i want to load the relevant html form and make the buttons disappear.

Android Studios / IntelliJ equivalent of "Add package folders to sources" feature in Netbeans

java netbeans intellij-idea android-studio android-gradle

In Netbeans, whenever I want to add an external folder (outside of the project directory) to my list of sources, I do something like this: