我有,有一个WebRequest的是一个POST增加了多次相同的密钥,从而使其成为一个数组在PHP,Java小等,我想改写这个来使用Web客户端眼中的.NET应用程序,但如果我叫Web客户端的QueryString.Add()使用相同的密钥多次,它只是附加了新的价值观,使值数组的一个逗号分隔的单个值代替。
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.
我使用后的WebClient的UploadFile()我的要求,因为除了这些元数据,我想一个文件发布。
I post my request using WebClient's UploadFile() because in addition to these metadata I want a file posted.
我如何使用Web客户端来发布值,而不是单个值的数组(逗号分隔值)?
How can I use WebClient to post an array of values instead of a single value (of comma-separated values)?
干杯
聂
PHP只是一个分析器转换以数组格式发送到一个数组多个值。
格式为< arrayName中><键>。
PHP simply uses a parser to convert multiple values sent with array format to an array.
The format is <arrayName>[<key>]
.
所以,如果你想从 $收到PHP数组_ GET
添加这些查询参数: X [键1]
和 X [密钥2]
。 $ _ GET ['X']
在PHP将是一个包含有2项: [X] =&GT;阵列(2){[键1] =&GT; &LT;凡是&GT; [键2] =&GT; &LT;凡是&GT; }
。
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++;
}
}
}
在code:
and in code:
webClient.AddArray("x", "1", "2", "3");
webClient.AddArray("x", "4");
或手动:
webClient.QueryString.Add("x[key1]", "4");
webClient.QueryString.Add("x[key2]", "1");
有没有错误检查,等等。你可以自己做:)
There is no error checking, etc. You can do it yourself :)