问题遇到的现象和发生背景
对方提供的接口要求提共的参数是JSON格式,C#如何提交POST类型的请示,总提示参数格式不对。
对方提供的接口要求提共的参数是JSON格式,C#如何提交POST类型的请示,总提示参数格式不对。
重点是你要确定对方要求的编码格式,头部你要声明你发送的数据收是json,代码如下:关于json你使用Text.Json和Newtonsoft.Json来格式化都是可以的
using System.Text;
string appId = "xxxxx";
string appSecret = "xxxx";
string TYSHXYDM = "892374832718943A";
string param = "{\"appId\":\"" + appId + "\",\"appSecret\":\"" + appSecret + "\",\"TYSHXYDM\":\"" + TYSHXYDM + "\"}";
string url = "https://xxxx.cn/api/v2/token/get";
var dict = new Dictionary<string, string>();
dict.Add("params", param);
using (var client = new HttpClient())
{
var response = await client.PostAsync(
url,
new FormUrlEncodedContent(dict));
var res = await response.Content.ReadAsStringAsync();
Console.WriteLine(res);
}