簡體   English   中英

使用 HttpClient C# 發送表單數據

[英]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.

這就是我在 postman 中發送請求的方式

在此處輸入圖像描述

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: **

postman 請求的提琴手跟蹤(工作正常)

在此處輸入圖像描述

C# 請求的提琴手跟蹤(失敗) 在此處輸入圖像描述

提前致謝!

問候, 莫克什

代替

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

嘗試創建一個新的 class:

public class RecipientSender
{
public string Recipient_First_Name {get; set;}
public string Recipient_Last_Name {get; set;}
.....
}

並像這樣創建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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM