2014-10-12 30 views
0

我想与具有相同名称的许多参数发送POST请求:发送POST请求与参数在C#中的同名

FormUrlEncodedContent content = new FormUrlEncodedContent(new [] { 
    new KeyValuePair < string, string > ("group_id", "344"), 
    new KeyValuePair < string, string > ("group_id", "20"), 
    new KeyValuePair < string, string > ("group_id", "456") 
}); 


HttpResponseMessage response = await _httpClient.PostAsync("http://localhost/api", content); 

但是,如果使用上述要求我只得到了第一个GROUP_ID响应( 344 ID)。你有什么想法我可以获得“group_id [] = 344 & group_id [] = 20 & group_id [] = 456”使用FormUrlEncodedContent

+0

您是否尝试过通过GROUP_ID关键和像这样的数组值[344,20,456]? – 2014-10-12 15:11:47

+0

我想是这样: 'FormUrlEncodedContent含量=新FormUrlEncodedContent(新[] { 新KeyValuePair <字符串,字符串[]>( “GROUP_ID”,新的字符串[] { “344”, “20”}) });' 但是,我得到的错误: 1. _最好的重载方法匹配'System.Net.Http.FormUrlEncodedContent.FormUrlEncodedContent(System.Collections.Generic.IEnumerable >)'有一些无效参数_ – pbalut 2014-10-12 16:58:02

回答

0

您正在使用具有相同键的多个键值对。 这是通常不会被允许的。 这些键值对将被忽略: new KeyValuePair<string, string>("group_id", "20"), new KeyValuePair<string, string>("group_id", "456")

期望这种输入的API必须更改其设计。

0

老问题,但我花了一些时间来弄清楚这一点,所以这里的答案:

你应该在你的钥匙使用括号像这样:

FormUrlEncodedContent content = new FormUrlEncodedContent(new [] { 
    new KeyValuePair < string, string > ("group_id[]", "344"), 
    new KeyValuePair < string, string > ("group_id[]", "20"), 
    new KeyValuePair < string, string > ("group_id[]", "456") 
});