using System.Net; using System.Text; using Newtonsoft.Json.Linq; namespace MES.Service.Dto.webApi.RealTimeInventory; public // HTTP客户端类,用于发送请求和获取响应 class HttpClientEx { public string Url { get; set; } public string Content { get; set; } static CookieContainer Cookie = new CookieContainer(); public string AsyncRequest() { HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest; httpRequest.Method = "POST"; httpRequest.ContentType = "application/json"; httpRequest.CookieContainer = Cookie; httpRequest.Timeout = 1000 * 60 * 10; // 10分钟超时 using (Stream reqStream = httpRequest.GetRequestStream()) { JObject jObj = new JObject { { "format", 1 }, { "useragent", "ApiClient" }, { "rid", Guid.NewGuid().ToString().GetHashCode().ToString() }, { "parameters", Content }, { "timestamp", DateTime.Now }, { "v", "1.0" } }; string 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(); // 返回响应内容 } } } }