c# - HttpClient PostAsync 无效的发布格式

标签 c# httpclient

我正在尝试使用 HttpClient 的 PostAsync 登录网站;然而,它总是失败,当我使用 WireShark 跟踪连接时,我发现它发布的数据不正确

代码

var content = new FormUrlEncodedContent(new[] 
{
    new KeyValuePair<string, string>("value1", data1),
    new KeyValuePair<string, string>("value2", data2),
    new KeyValuePair<string, string>("value3", data3)
});

var content = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("value1", data1), 
    new KeyValuePair<string, string>("value2", data2), 
    new KeyValuePair<string, string>("value3", data3)
};

用法

httpClient.PostAsync(postUri, content)

期望

value1=123456&value2=123456&value3=123456

现实

//It adds strange += which makes the post fail...
value1=123456&value2+=123456&value3+=123456

最佳答案

我知道这行得通:

var values = new List<KeyValuePair<string, string>>();

values.Add(new KeyValuePair<string, string>("Item1", "Value1"));
values.Add(new KeyValuePair<string, string>("Item2", "Value2"));
values.Add(new KeyValuePair<string, string>("Item3", "Value3"));

using (var content = new FormUrlEncodedContent(values))
{
    client.PostAsync(postUri, content).Result)
}

关于c# - HttpClient PostAsync 无效的发布格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17973981/

相关文章:

c# - 为什么从 XDocument 获取元素会导致空值?

api - 具有授权的 Node JS POST 方法

asp.net-mvc-4 - 使用 ReadAsAsync<T>() 反序列化复杂的 Json 对象

Angular http 客户端 retryWhen 无法捕获调用者的错误

java - 如何显示dailymotion云上传的进度条

C# CA2104 - 自动代码分析不喜欢静态只读可变类型

c# - 在 SelectMany 方面需要一些帮助

c# - 使用 MVVM,ContextMenu ViewModel 如何找到打开 ContextMenu 的 ViewModel?

c# - 以DateTime为目标的调试可视化器不会传输该值

azure - 每次在静态 HttpClient 上清除 DefaultRequestHeaders 是否会导致 Azure Function 应用程序出现问题?