【发布时间】:2019-03-14 21:26:23
【问题描述】:
我创建了一个 KeyValuePair 列表来填充内容作为 HttpClient 的数据。
List<KeyValuePair<string, string>> keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("email", email));
keyValues.Add(new KeyValuePair<string, string>("password", password));
keyValues.Add(new KeyValuePair<string, string>("plan_id", planId));
var content = new FormUrlEncodedContent(keyValues);
但后来我发现我必须发送一个 int 值作为 plan_id。如何更改上述列表以接受 KeyValuePair。或者有没有更好的方法来做到这一点?
【问题讨论】:
-
最终,HTTP 中的所有内容都作为 ASCII 字符串发送,那么为什么不直接调用
planId.ToString(CultureInfo.InvariantCulture)? -
我正在调用外部 API。当我为计划 ID 发送字符串值时,我收到了错误请求。但有了 int 值,它就成功了。
-
是否可以在 java 中创建如下通用列表。 List
> keyValues = new List >(); -
好吧,
FormUrlEncodedContent只会接受IEnumerable<KeyValuePair<string, string>> nameValueCollection,所以除非您对内容进行不同的编码,否则您必须这样做,正如@Dai 所提到的,将您的整数转换为其字符串表示形式。 -
感谢@Dai 和 Alex.. 这行得通..
标签: c# list generics httpclient keyvaluepair