tags:

views:

866

answers:

3

I'm developing an ASP.Net handler with C# that receives an array of data posted from an external Flash program. How can I use the data as array? It seems the HTTP Context used in ASP.Net automatically html-decodes the post data so that it comes out as a simple string. The separator in this case is comma (could be it's always a comma, never worked with this before).

Seems I need either of two things:

1) A way to get to the data in its html-encoded form, where the comma used as a separator for the arrays can ONLY represent real arrays instead of customer form input (the customer input comma would remain encoded at this point).

2) A way to simulate the PHP print_r(), var_dump() function (don't know PHP myself, but I'm told that does the trick there) to dump the variable into an array.

So any help on how to do either would be appreciated. Thanks in advance!

Edit 1: The data being posted can be for example a bunch of addresses, postalcodes and optional extra infos. Such as "address=testroad%5F5,another%5Ftestroad%5F6&postalcode=12345,56789&extrainfos=,firstwasempty". In that example, there were two addresses (%5F equals whitespace), two postalcodes, but only the second address contained extrainfo as the space before the comma was empty. Now once more, as English isn't my mothertongue, the problem was that possible customer-written comma gets mixed with the actual array-splitting comma after decoding.

A: 

Check HttpRequest.InputStream property: http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx

deerchao
+2  A: 
public void ProcessRequest(HttpContext context)
{
    using (var reader = new StreamReader(context.Request.InputStream))
    {
        string postedData = reader.ReadToEnd();
        foreach (var item in postedData.Split(new [] { '&' }, StringSplitOptions.RemoveEmptyEntries))
        {
            var tokens = item.Split(new [] { '=' }, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length < 2)
            {
                continue;
            }
            var paramName = tokens[0];
            var paramValue = tokens[1];
            var values = paramValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var value in values)
            {
                var decodedValue = context.Server.UrlDecode(value);
                // Do something with the decoded value which corresponds to paramName
            }
        }
    }
}
Darin Dimitrov
Thanks for this, it does get the data. Problem is parsing it as you guessed. The data I'm trying to get at is a bunch of addresses, for example "address", "postalcode" etc. It gets troublesome parsing that with for loops and substrings. So is there a way to get the data as form or "querystring" without decoding the data inside?
Kahn
I've updated my answer with an example showing how you could use String.Split to parse the posted data.
Darin Dimitrov
A: 

I found

Request.Params[null]

refers to the RAW data posted to the page in C# ASP.NET.

DEF