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(); // 返回响应内容 } } } }