Process raw HTTP request content

asp.nete-commercehttp

I am doing an e-commerce solution in ASP.NET which uses PayPal's Website Payments Standard service. Together with that I use a service they offer (Payment Data Transfer) that sends you back order information after a user has completed a payment. The final thing I need to do is to parse the POST request from them and persist the info in it. The HTTP request's content is in this form :

SUCCESS
first_name=Jane+Doe
last_name=Smith
payment_status=Completed
payer_email=janedoesmith%40hotmail.com
payment_gross=3.99
mc_currency=USD
custom=For+the+purchase+of+the+rare+book+Green+Eggs+%26+Ham

Basically I want to parse this information and do something meaningful, like send it through e-mail or save it in DB. My question is what is the right approach to do parsing raw HTTP data in ASP.NET, not how the parsing itself is done.

Best Solution

Something like this placed in your onload event.

if (Request.RequestType == "POST")
{
    using (StreamReader sr = new StreamReader(Request.InputStream))
    {
        if (sr.ReadLine() == "SUCCESS")
        {
            /* Do your parsing here */
        }
    }
}

Mind you that they might want some special sort of response to (ie; not your full webpage), so you might do something like this after you're done parsing.

Response.Clear();
Response.ContentType = "text/plain";
Response.Write("Thanks!");
Response.End();

Update: this should be done in a Generic Handler (.ashx) file in order to avoid a great deal of overhead from the page model. Check out this article for more information about .ashx files