[英]Send form-data using HttpClient C#
I'm trying to make an API call to third party URL, which is working fine through postman, but same request through C# HttpClient not working. I'm trying to make an API call to third party URL, which is working fine through postman, but same request through C# HttpClient not working.
This is how I'm sending request in postman
这就是我在 postman 中发送请求的方式
C# Sample Code
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)
};
Am I doing anything wrong here?我在这里做错什么了吗?
UPDATE #1: **更新#1: **
Fiddler Traces for postman request(which is working fine)
postman 请求的提琴手跟踪(工作正常)
Fiddler Traces for C# Request(which is failing)
C# 请求的提琴手跟踪(失败)
Thanks in advance!提前致谢!
Regards, Moksh问候, 莫克什
Instead of代替
var nvc = new List<KeyValuePair<string, string>>();
try to create a new class:尝试创建一个新的 class:
public class RecipientSender
{
public string Recipient_First_Name {get; set;}
public string Recipient_Last_Name {get; set;}
.....
}
and create httpclientrequest like this:并像这样创建httpclientrequest:
....
....
var recipientSender=new RecipientSender { Recipient_FirstName=..., }
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
httpClient.DefaultRequestHeaders.Accept.Add(contentType);
var stringData = JsonConvert.SerializeObject(recipientSender);
contentData = new StringContent(stringData, Encoding.UTF8, "application/json");
var httpResponseMessage=client.PostAsync(url, contentData);
.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.