Encryption
Encryption

Reputation: 1897

How to View HTTPRequest information

I'm designing a web interface that is responsible for communicating with a piece of hardware. The hardware contacts the web page and sends an HTTP Request using POST that contains specific data in the body of http package using a "key=value" format.

Server-side I have code that does the following:

public override void ProcessRequest(HttpContext curContext)
    {
        if (curContext != null)
        {
            HttpResponse response = curContext.Response;
            response.Clear();
            response.ContentType = "text/plain";
            response.BufferOutput = true;
            response.StatusCode = 200; // HttpStatusCode.OK;
            response.Write(response.StatusCode.ToString());
            response.End();
        }
    }

But what I really need is the necessary code to review the Body and retrieve the data (which is in text/plain format). I'm fairly new to this type of web programming so I don't know exactly what code to write to look in the curContext to get this information, or if I even have my override method correct.

I was expecting to have something available like curContext.Request.Body but this isn't the case. How can I see the raw POST data in body of the Request? Can anyone point me in the right direction?

Thanks

Upvotes: 2

Views: 413

Answers (2)

casperOne
casperOne

Reputation: 74530

You'll want to use the InputStream property on the HttpRequest instance returned by the Request property exposed by the HttpContext passed to your ProcessRequest method.

This will give you the contents of the POST request (which you can verify with a call to the HttpMethod property on the same HttpRequest).

Note that you'll have to use a StreamReader in order to convert the byte stream into strings which you'd then decode (since those key/value pairs should be url-encoded).

Fortunately, this is already done for you. On the HttpRequest instance, you can use the NameValueCollection returned by the Form property to check for the values passed as part of a POST request with url-encoded key/value pairs like so:

public override void ProcessRequest(HttpContext curContext)
{
    if (curContext != null) return;

    string value = curContext.Form["key"];

    // Do other processing.
    ...

Note that you can also use the indexer on the HttpRequest, but that combines everything exposed by the QueryString, Form, Cookies and ServerVariables collections which I would say in this case is a bad idea, as you are specifically posting information and expecting that on the other side.

Upvotes: 2

Brian Ball
Brian Ball

Reputation: 12596

If you are putting the data in the body of the request in "key=value" format, it sounds like you are URL encoding form data (as long as you are delimiting the key/value pairs with an ampersand (&).

If that's the case, you should be able to use:

curContext.Request["key"]

And it will give you "value" in your example.

Upvotes: 0

Related Questions