11
啊鑫
2025-07-15 5dc3cc86f5835369cd830f2a83318b9a8d69cf69
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
 
namespace MES.Service.Dto.webApi.RealTimeInventory;
 
public
    // HTTP客户端类,用于发送请求和获取响应
    class HttpClientEx
{
    private static readonly CookieContainer Cookie = new();
    public string Url { get; set; }
    public string Content { get; set; }
 
    public string AsyncRequest()
    {
        var httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;
        httpRequest.Method = "POST";
        httpRequest.ContentType = "application/json";
        httpRequest.CookieContainer = Cookie;
        httpRequest.Timeout = 1000 * 60 * 10; // 10分钟超时
 
        using (var reqStream = httpRequest.GetRequestStream())
        {
            var jObj = new JObject
            {
                { "format", 1 },
                { "useragent", "ApiClient" },
                { "rid", Guid.NewGuid().ToString().GetHashCode().ToString() },
                { "parameters", Content },
                { "timestamp", DateTime.Now },
                { "v", "1.0" }
            };
 
            var sContent = jObj.ToString();
            var bytes = UnicodeEncoding.UTF8.GetBytes(sContent);
            reqStream.Write(bytes, 0, bytes.Length);
            reqStream.Flush();
        }
 
        using (var repStream = httpRequest.GetResponse().GetResponseStream())
        {
            using (var reader = new StreamReader(repStream))
            {
                return reader.ReadToEnd(); // 返回响应内容
            }
        }
    }
}