C# HttpClient Post 请求示例代码
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// 创建 HttpClient 实例
using (HttpClient client = new HttpClient())
{
// 设置请求的地址
string url = "http://example.com/api/endpoint";
// 构造要发送的数据
var data = new { Name = "John", Age = 30 };
// 发送 POST 请求
HttpResponseMessage response = await client.PostAsJsonAsync(url, data);
// 检查响应是否成功
if (response.IsSuccessStatusCode)
{
// 读取响应内容
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine("响应内容: " + result);
}
else
{
Console.WriteLine("请求失败: " + response.StatusCode);
}
}
}
}
来源:CSDN AI【C知道】

浙公网安备 33010602011771号