[英]How to POST using HTTPclient content type = application/x-www-form-urlencoded
[英]use of application/x-www-form-urlencoded in POST method, HttpClient
如果我要发送此数据怎么办:发布参数:
access_token access_token_Value
list数组数组(有4对arg)
arg1 arg1_value
arg2 arg2_value
arg3 arg3_value
arg4 arg4_value
在规范中是这样的(POST参数和返回数组中的所有可能的枚举值都在本文档中指定,并用竖线||分隔)。 我有通用Windows应用程序项目。 如何将此数据转换为“application / x-www-form-urlencoded”格式? 对于我使用的键值这样的普通对
var body = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("arg1", "arg1value"),
new KeyValuePair<string, string>("arg2", "arg2value"),
new KeyValuePair<string, string>("arg3", "arg3value"),
new KeyValuePair<string, string>("arg4", "arg4value")
};
var content = new FormUrlEncodedContent(body);
var result = httpClient.PostAsync(uri, content).Result;
这是好的(传输数据:arg1 = arg1value&arg2 = arg2value&....),但如果数据与我在本文开头写的相同怎么办?
假设您的密钥不需要编码(即不包含URI中特殊的任何字符),其中名称 - 值对集合中的字段已经转换为字符串):
var httpBody = String.Join('&',
fields.Select(nv =>
String.Concat(nv.Name,
"=",
WebUtility.UrlEncode(nv.Value))));
然后使用适用的编码序列化到HttpWebRequest
主体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.