From f5b8c1ae89286dada20ea433ffac84f4c9e72a29 Mon Sep 17 00:00:00 2001 From: huanghongfeng <huanghongfeng@hnkhzn.com> Date: 星期五, 06 六月 2025 14:31:25 +0800 Subject: [PATCH] 1 --- 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/HttpHelper.cs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 57 insertions(+), 1 deletions(-) diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/HttpHelper.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/HttpHelper.cs" index 77224e9..b4b51b2 100644 --- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/HttpHelper.cs" +++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/HttpHelper.cs" @@ -1,6 +1,8 @@ -锘縰sing System; +锘縰sing Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; @@ -100,5 +102,59 @@ } return null; } + /// <summary> + /// post璇锋眰 + /// </summary> + /// <param name="url"></param> + /// <param name="parm">鍙傛暟</param> + /// <param name="rquestName">鎺ュ彛鍚嶇О,鐢ㄤ簬鏃ュ織鍒嗙被</param> + /// <returns></returns> + public static T Post<T>(string url, object parm, string rquestName = "") where T : class + { + HttpWebResponse response = null; + StreamReader resultReader = null; + string responseContent = string.Empty; + try + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "POST"; + request.ContentType = "application/json; charset=UTF-8"; + parm = parm ?? ""; + byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(parm)); + request.ContentLength = data.Length; + using (Stream newStream = request.GetRequestStream()) + { + newStream.Write(data, 0, data.Length); + }; + + response = (HttpWebResponse)request.GetResponse(); + Stream webStream = response.GetResponseStream(); + if (webStream == null) + { + throw new Exception("Network error"); + } + + int statsCode = (int)response.StatusCode; + resultReader = new StreamReader(webStream, Encoding.UTF8); + responseContent = resultReader.ReadToEnd(); + + if (response != null) + response.Close(); + if (resultReader != null) + resultReader.Close(); + + if (statsCode != 200) + { + throw new Exception("寮傚父锛屽搷搴旂爜锛�" + statsCode.ToString()); + } + + return JsonConvert.DeserializeObject<T>(responseContent); + } + catch (Exception ex) + { + throw ex; + } + } + } } -- Gitblit v1.9.3