2014-10-12 91 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") 
});