zjh
2025-11-17 cfb616bf02b554b185f04bf92a8b8af489490102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.IO;
using System.Net;
using System.Text;
 
public static class HttpHelper
{
    /// <summary>
    /// 发送 POST 请求(Content-Type: application/json)
    /// </summary>
    public static string HttpPost(string url, string jsonBody)
    {
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/json;charset=UTF-8";
 
        byte[] data = Encoding.UTF8.GetBytes(jsonBody);
        request.ContentLength = data.Length;
 
        // 写入 Body
        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }
 
        // 接收响应
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
}