views:

1449

answers:

6

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
firstname=Jane+Doe
last
name=Smith
paymentstatus=Completed
payer
email=janedoesmith%40hotmail.com
paymentgross=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.

A: 

If I'm reading your question right, I think you're looking for the InputStream property on the Request object. Keep in mind that this is a firehose stream, so you can't reset it.

Greg Hurlman
+1  A: 

Well if the incoming data is in a standard form encoded POST format, then using the Request.Form array will give you all the data in a nice to handle manner.

If not then I can't see any way other than using Request.InputStream.

samjudson
+2  A: 

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

Markus Olsson
+2  A: 

Use an IHttpHandler and avoid the Page model overhead (which you don't need), but use Request.Form to get the values so you don't have to parse name value pairs yourself. Just pretend you're in PHP or Classic ASP (or ASP.NET MVC, for that matter). ;)

Mark Brackett
A: 

@Mark Brackett

Or probably write a RESTful web service? I'm going for the simplest solution here, and I'll stick to the Page overhead because I need to display a page afterwards anyway. And I cannot pretend to be in PHP or ASP because I never went deep in any of them. Thanks for the suggestion, though. It is in fact much better, but would require a little more coding. I'm lazy :)

Slavo
+2  A: 

I'd strongly recommend saving each request to some file.

This way, you can always go back to the actual contents of it later. You can thank me later, when you find that hostile-endian, koi-8 encoded, [...], whatever it was that stumped your parser...

Anders Eurenius