我正在尝试使用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)
});
Run Code Online (Sandbox Code Playgroud)
要么
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)
};
Run Code Online (Sandbox Code Playgroud)
用法
httpClient.PostAsync(postUri, content)
Run Code Online (Sandbox Code Playgroud)
value1=123456&value2=123456&value3=123456
Run Code Online (Sandbox Code Playgroud)
//It adds strange += which makes the post fail...
value1=123456&value2+=123456&value3+=123456
Run Code Online (Sandbox Code Playgroud)
我知道这有效:
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)
}
Run Code Online (Sandbox Code Playgroud)