views:

29

answers:

1

Hi guys,

I've got a .net application that has a WebRequest that to a POST adds multiple times the same key, thus making it an array in the eyes of PHP, Java Servlets etc. I wanted to rewrite this to using WebClient, but if I call WebClient's QueryString.Add() with the same key multiple times, it just appends the new values, making a comma-separated single value instead of an array of values.

I post my request using WebClient's UploadFile() because in addition to these metadata I want a file posted.

How can I use WebClient to post an array of values instead of a single value (of comma-separated values)?

Cheers

Nik

A: 

PHP simply uses a parser to convert multiple values sent with array format to an array. The format is <arrayName>[<key>].

So if you want to receive an array in PHP from $_GET add these query parameters: x[key1] and x[key2]. $_GET['x'] in PHP will be an array with 2 items: ["x"]=> array(2) { ["key1"]=> <whatever> ["key2"]=> <whatever> }.

Edit - you can try this extension method:

public static class WebClientExtension
{
    public static void AddArray(this WebClient webClient, string key, params string[] values)
    {
        int index = webClient.QueryString.Count;

        foreach (string value in values)
        {
            webClient.QueryString.Add(key + "[" + index + "]", value);
            index++;
        }
    }
}

and in code:

webClient.AddArray("x", "1", "2", "3");
webClient.AddArray("x", "4");

or manually:

webClient.QueryString.Add("x[key1]", "4");
webClient.QueryString.Add("x[key2]", "1");

There is no error checking, etc. You can do it yourself :)

Jaroslav Jandek
Correct, that is how PHP parses an array. But WebClient, as far as I know, will instead give ["x"] = "whatever, whatever", which is not what I want to send. Do you have any suggestions how I make WebClient behave correctly?
niklassaers
@niklassaers: with `webClient.QueryString.Add("x[key1]", "4");` and `webClient.QueryString.Add("x[key2]", "3");` I always end-up with an array in `$_GET`. My PHP version is 5.3.2.
Jaroslav Jandek
Thanks for the tip, I'll give that a whirl monday. :-)
niklassaers
Hmm, I might be misunderstanding you. I did webClient.QueryString.Add("x[0]", "1"); webClient.QueryString.Add("x[1]", "2"); webClient.QueryString.Add("x[2]", "3"); but this only gave me three keys with each a single value, not one key with an array of the values [1, 2, 3]. How do you suggest I with WebClient get an array where the name is x and the value is [1, 2, 3], not the string "1, 2, 3" that WebClient gives me when I do webClient.QueryString.Add("x", "1"); webClient.QueryString.Add("x", "2"); webClient.QueryString.Add("x", "3");
niklassaers
You are posting to PHP, right? So in PHP you get an array. WebClient does not support it so you can't "get" array in the query string, you can only post an array (using the above way) to PHP. You would need to create your own WebClient or a QueryString wrapper that would do it for you - which is IMHO waste of time when you can do it very simply with the methods I posted... No http system posts arrays, only single values - it gets converted to an array on the receiving end (PHP). If you want to receive arrays in .NET, you need to parse the response yourself!
Jaroslav Jandek
Also, if you simply post with the method I provided to a PHP page, you will see it works as advertised...
Jaroslav Jandek