using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // 创建HttpClient实例
        using (HttpClient client = new HttpClient())
        {
            // 设置请求地址
            string url = "https://example.com/your-api-endpoint";

            // 创建表单参数
            var parameters = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("page", "1"),
                new KeyValuePair<string, string>("param1", ""),
                new KeyValuePair<string, string>("param2", ""),
            };

            // 创建表单内容
            var content = new FormUrlEncodedContent(parameters);

            // 发送POST请求
            HttpResponseMessage response = await client.PostAsync(url, content);

            // 确认响应状态
            if (response.IsSuccessStatusCode)
            {
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine("请求成功,响应内容:");
                Console.WriteLine(responseBody);
            }
            else
            {
                Console.WriteLine("请求失败,状态码: " + response.StatusCode);
            }
        }
    }
}
最后修改:2024 年 06 月 18 日
如果觉得我的文章对你有用,请随意赞赏