Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get POST data as array with C#

Tags:

arrays

c#

post

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 info. Such as address=testroad%5F5,another%5Ftestroad%5F6&postalcode=12345,56789&extrainfo=,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.

like image 398
Kahn Avatar asked Dec 01 '09 07:12

Kahn


2 Answers

I had the same trouble and found this post, but just now I've found a better way to do it.

You should put the same name on all the POST variables, so the data to be posted is:

address=AddressValue1&address=AddressValue2&address=AddressValue3

In the C# code script you can then access the data parameters with:

string[] addresses = this.Request.Form.GetValues("address");
foreach (string address in addresses)
{
    // Your code here ...
}
like image 118
Jota Santos Avatar answered Oct 02 '22 18:10

Jota Santos


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
            }
        }
    }
}
like image 39
Darin Dimitrov Avatar answered Oct 02 '22 18:10

Darin Dimitrov