Bootstrap

C#调用Post接口,参数选择Body中的form-data,然后content-type为application/x-www-form-urlencoded,参数个数自己修改

using System;
using System.Collections.Generic;

public class HttpPostSender
{
    public string SendPostRequest(string url, IDictionary<string, string> formDataValues)
    {
        using (var client = new System.Net.Http.HttpClient())
        {
            var formData = new System.Net.Http.FormUrlEncodedContent(formDataValues);
            var response = client.PostAsync(url, formData).Result;

            if (response.IsSuccessStatusCode)
            {
                return response.Content.ReadAsStringAsync().Result;
            }
            else
            {
                Console.WriteLine("Error: " + response.StatusCode);
                return string.Empty;
            }
        }
    }
}

// 在其他类中调用 HttpPostSender 类
public class OtherClass
{
    public void CallHttpPostSender()
    {
        var httpPostSender = new HttpPostSender();
        
        //根据实际需求来传参数
        var formDataValues = new Dictionary<string, string>
        {
            { "param1", "value1" },
            { "param2", "value2" },
            { "param3", "value3" },
            { "param4", "value4" }
        };

        string url = "https://example.com/api/endpoint";

        string responseContent = httpPostSender.SendPostRequest(url, formDataValues);

        Console.WriteLine(responseContent);
    }
}

;