c# post json

纯净搜索 为您找到 本页 4 条,用时 0 秒(数据源:360)

AI 回答 由 glm-4-flash 生成 · 仅供参考,请自行核实
1. 使用 `HttpClient` 类发送 POST 请求。
2. 创建 `HttpContent` 对象,将 JSON 数据转换为 `StringContent`。
3. 设置 `StringContent` 的 `MediaType` 为 `application/json`。
4. 发送 POST 请求,获取响应。

using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

public async Task<string> SendPostRequest(string url, string jsonData)
{
using (HttpClient client = new HttpClient())
{
HttpContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
return await response.Content.ReadAsStringAsync();
}
}