【发布时间】:2021-03-16 19:46:52
【问题描述】:
我正在尝试对第三方 URL 进行 API 调用,通过邮递员可以正常工作,但通过 C# HttpClient 的相同请求不起作用。
这就是我在邮递员中发送请求的方式
C# 示例代码
var nvc = new List<KeyValuePair<string, string>>();
nvc.Add(new KeyValuePair<string, string>("PARAM1", "PARAM1 Value"));
nvc.Add(new KeyValuePair<string, string>("PARAM2", "PARAM2 Value"));
nvc.Add(new KeyValuePair<string, string>("PARAM3", "PARAM3 Value"));
var req = new HttpRequestMessage(HttpMethod.Post, "Https://ThirdPartyURL") {
Content = new FormUrlEncodedContent(nvc)
};
我在这里做错了吗?
更新 #1: **
邮递员请求的 Fiddler Traces(工作正常)
提前致谢!
问候, 莫克什
【问题讨论】:
-
我看到您使用的是
FormUrlEncodedContent,并且在 Postman 中选择了form-data。FormUrlEncodedContent发送Content-Type: application/x-www-form-urlencoded; <charset>和 form-data 我相信应该是Content-Type: multipart/form-data; boundary=<value> -
@dcg 看起来这是真正的问题,我刚刚验证了邮递员和 C# 的提琴手跟踪并发现了同样的事情,你有示例代码,比如我们如何在此处设置内容类型?
-
我只是在看this的答案,我想它可能对你有帮助。
-
另外,看看this。有
MultipartFormDataContent类。 -
@dcg 好吧,之前我试过
MultipartFormDataContent,但那是抛出 400 错误请求。
标签: c# httpclient form-data rest zoho