-2
  • 将来自POSTMAN的数据作为x-www-form-urlencoded传递
  • 键和值如下:
数据:P1;P2
格式:json

来自 POSTMAN 的相应 curl 代码

curl --location --request POST 'https://ap-url/id/' \
--header '内容类型:应用程序/x-www-form-urlencoded' \
--data-urlencode '数据=P1;P2' \

如何在 HttpClient 上以 x-www-form-urlencoded 格式发送数据?

4

1 回答 1

2
  1. https://curl.olsh.me/用于 C# 代码的 curl 命令
using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api-url/id"))
    {
        var contentList = new List<string>();
        contentList.Add($"data={Uri.EscapeDataString("P1;P2")}");
        contentList.Add($"format={Uri.EscapeDataString("json")}");
        request.Content = new StringContent(string.Join("&", contentList));
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded"); 

        var response = await httpClient.SendAsync(request);
    }
}
于 2020-06-29T20:44:14.140 回答