From 382c009ba2bb04374468e9584f262d6682841d9b Mon Sep 17 00:00:00 2001 From: pengmn <pmn@HanInfo> Date: 星期三, 28 五月 2025 18:09:41 +0800 Subject: [PATCH] 杭氧测试流程 --- HH.WCS.Mobox3.HangYang/util/HttpHelper.cs | 70 ++++++++++++++++++++++++----------- 1 files changed, 48 insertions(+), 22 deletions(-) diff --git a/HH.WCS.Mobox3.HangYang/util/HttpHelper.cs b/HH.WCS.Mobox3.HangYang/util/HttpHelper.cs index 7e1a661..f0970dc 100644 --- a/HH.WCS.Mobox3.HangYang/util/HttpHelper.cs +++ b/HH.WCS.Mobox3.HangYang/util/HttpHelper.cs @@ -16,6 +16,7 @@ WebRequest request = WebRequest.Create(url); request.Method = "POST"; + if (!string.IsNullOrEmpty(AppKey) && !string.IsNullOrEmpty(ReqTime) && !string.IsNullOrEmpty(ReqVerify)) { request.Headers.Set("AppKey", AppKey); @@ -53,7 +54,9 @@ #endregion - public string WebPost(string url, string postData, string cotentType = "application/json") { + #region WebHelper + public string WebPost(string url, string postData, string cotentType = "application/json") + { Console.WriteLine(url); WebRequest request = WebRequest.Create(url); request.Method = "POST"; @@ -63,7 +66,8 @@ request.ContentLength = byteArray.Length; request.Timeout = 3000; - try { + try + { Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); @@ -77,13 +81,15 @@ response.Close(); return responseFromServer; } - catch (Exception e) { + catch (Exception e) + { Console.WriteLine(e.Message); return ""; } } - public string WebGet(string url) { + public string WebGet(string url) + { //using (var client = new HttpClient()) { // //璇锋眰缁撴灉 // string result = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result; @@ -97,7 +103,8 @@ request.Timeout = 6000; request.Method = "GET"; - try { + try + { WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); @@ -109,29 +116,34 @@ Console.WriteLine(responseFromServer); return responseFromServer; } - catch (Exception e) { + catch (Exception e) + { return ""; } } - public string Get(string url, string contentType = "application/x-www-form-urlencoded") { + public string Get(string url, string contentType = "application/x-www-form-urlencoded") + { WebRequest request = WebRequest.Create(url); request.Method = "Get"; request.ContentType = contentType; StreamReader reader = null; Stream stream = null; WebResponse rsp = null; - try { + try + { rsp = request.GetResponse(); stream = rsp.GetResponseStream(); reader = new StreamReader(stream); return reader.ReadToEnd(); } - catch { + catch + { return ""; } - finally { + finally + { // 閲婃斁璧勬簮 if (reader != null) reader.Close(); if (stream != null) stream.Close(); @@ -140,7 +152,8 @@ } - public string Post(string url, string postData, string contentType = "application/json", string sessionId = "") { + public string Post(string url, string postData, string contentType = "application/json", string sessionId = "") + { Console.WriteLine(url); WebRequest request = WebRequest.Create(url); request.Method = "POST"; @@ -148,13 +161,15 @@ request.ContentType = contentType; request.ContentLength = byteArray.Length; request.Timeout = 3000; - if (sessionId != "") { + if (sessionId != "") + { request.Headers.Set("ASP.NET_SessionId", sessionId); } StreamReader reader = null; Stream stream = null; WebResponse rsp = null; - try { + try + { stream = request.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); @@ -163,11 +178,13 @@ reader = new StreamReader(stream); return reader.ReadToEnd(); } - catch (Exception ex) { + catch (Exception ex) + { Console.WriteLine($"{url} err={ex.Message}"); return ""; } - finally { + finally + { // 閲婃斁璧勬簮 if (reader != null) reader.Close(); if (stream != null) stream.Close(); @@ -177,29 +194,34 @@ } - public string Post(string url, Dictionary<string, string> dic) { + public string Post(string url, Dictionary<string, string> dic) + { var param = dic.Select(a => { return string.Format("{0}={1}", a.Key, a.Value); }).ToList(); return Post(url, string.Join("&", param), "application/x-www-form-urlencoded"); } - public string PostWithCookie(string url, string sessinId) { + public string PostWithCookie(string url, string sessinId) + { return PostWithCookie(url, "", "", sessinId); } - public string PostWithCookie(string url, string postData, string contentType = "application/json", string sessionId = "") { + public string PostWithCookie(string url, string postData, string contentType = "application/json", string sessionId = "") + { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = contentType; request.ContentLength = byteArray.Length; - if (sessionId != "") { + if (sessionId != "") + { request.CookieContainer = new CookieContainer(); request.CookieContainer.SetCookies(new Uri("http://" + request.RequestUri.Authority), "ASP.NET_SessionId=" + sessionId); } StreamReader reader = null; Stream stream = null; WebResponse rsp = null; - try { + try + { stream = request.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); @@ -208,10 +230,12 @@ reader = new StreamReader(stream); return reader.ReadToEnd(); } - catch { + catch + { return ""; } - finally { + finally + { // 閲婃斁璧勬簮 if (reader != null) reader.Close(); if (stream != null) stream.Close(); @@ -219,6 +243,8 @@ } } + #endregion + } -- Gitblit v1.9.1