pengmn
2025-05-30 cd40ada4efe0d0a4036714cf597ce170b8cf5a54
HH.WCS.Mobox3.HangYang/util/HttpHelper.cs
@@ -6,18 +6,31 @@
using System.Text;
namespace HH.WCS.HangYang.util {
    public class HttpHelper {
        public string WebPost(string url, string postData, string cotentType = "application/json") {
    public class HttpHelper
    {
        #region  杭氧WebHelper
        public string WebPost(string url, string postData, string cotentType = "application/json", string AppKey = "", string ReqTime = "", string ReqVerify = "")
        {
            Console.WriteLine(url);
            WebRequest request = WebRequest.Create(url);
            request.Method = "POST";
            if (!string.IsNullOrEmpty(AppKey) && !string.IsNullOrEmpty(ReqTime) && !string.IsNullOrEmpty(ReqVerify))
            {
                request.Headers.Set("AppKey", AppKey);
                request.Headers.Set("ReqTime", ReqTime);
                request.Headers.Set("ReqVerify", ReqVerify);
            }
            //string postData = JsonConvert.SerializeObject(data); ;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = cotentType;
            request.ContentLength = byteArray.Length;
            request.Timeout = 3000;
            try {
            try
            {
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
@@ -31,13 +44,52 @@
                response.Close();
                return responseFromServer;
            }
            catch (Exception e) {
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return "";
            }
        }
        public string WebGet(string url) {
        #endregion
        #region WebHelper
        public string WebHttpPost(string url, string postData, string cotentType = "application/json")
        {
            Console.WriteLine(url);
            WebRequest request = WebRequest.Create(url);
            request.Method = "POST";
            //string postData = JsonConvert.SerializeObject(data); ;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = cotentType;
            request.ContentLength = byteArray.Length;
            request.Timeout = 3000;
            try
            {
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                WebResponse response = request.GetResponse();
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
                string responseFromServer = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
                response.Close();
                return responseFromServer;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return "";
            }
        }
        public string WebGet(string url)
        {
            //using (var client = new HttpClient()) {
            //   //请求结果
            //   string result = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
@@ -51,7 +103,8 @@
            request.Timeout = 6000;
            request.Method = "GET";
            try {
            try
            {
                WebResponse response = request.GetResponse();
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
@@ -63,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();
@@ -94,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";
@@ -102,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();
@@ -117,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();
@@ -131,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();
@@ -162,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();
@@ -173,6 +243,8 @@
            }
        }
        #endregion
    }