您可以使用StringContent类将JSON参数转换为application/x-www-form-urlencoded格式的字符串内容。下面是一个示例代码:

using System;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        // 创建一个包含JSON参数的对象
        var data = new
        {
            Name = "John",
            Age = 30,
            Email = "john@example.com"
        };

        // 将对象转换为JSON字符串
        var json = JsonConvert.SerializeObject(data);

        // 创建一个StringContent对象,将JSON字符串作为内容
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        // 输出StringContent对象的内容
        Console.WriteLine(content.ReadAsStringAsync().Result);
    }
}

在这个示例中,我们首先定义了一个包含JSON参数的匿名对象data,然后使用JsonConvert.SerializeObject方法将其转换为JSON字符串。接下来,我们使用StringContent类创建一个content对象,将JSON字符串作为内容,并指定编码和内容类型。最后,我们通过调用ReadAsStringAsync方法来获取content对象的内容并输出。


原文地址: https://www.cveoy.top/t/topic/pJFh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录