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
You’ll want to use the
InputStreamproperty on theHttpRequestinstance returned by theRequestproperty exposed by theHttpContextpassed to yourProcessRequestmethod.This will give you the contents of the
POSTrequest (which you can verify with a call to theHttpMethodproperty on the sameHttpRequest).Note that you’ll have to use a
StreamReaderin 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
HttpRequestinstance, you can use theNameValueCollectionreturned by theFormproperty to check for the values passed as part of aPOSTrequest with url-encoded key/value pairs like so:Note that you can also use the indexer on the
HttpRequest, but that combines everything exposed by theQueryString,Form,CookiesandServerVariablescollections which I would say in this case is a bad idea, as you are specifically posting information and expecting that on the other side.