From 6992dfd5cd06481ee251e72b6d28e0221ea9276b Mon Sep 17 00:00:00 2001
From: 陈嘉乐 <cjl@hdlchina.com.cn>
Date: 星期五, 04 十二月 2020 16:04:45 +0800
Subject: [PATCH] 2020-12-04-3
---
HDL_ON/DAL/Server/HttpUtil.cs | 558 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 558 insertions(+), 0 deletions(-)
diff --git a/HDL_ON/DAL/Server/HttpUtil.cs b/HDL_ON/DAL/Server/HttpUtil.cs
new file mode 100644
index 0000000..5e52242
--- /dev/null
+++ b/HDL_ON/DAL/Server/HttpUtil.cs
@@ -0,0 +1,558 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Security.Cryptography;
+using System.Text;
+using HDL_ON.Entity;
+using RestSharp;
+using Shared;
+
+namespace HDL_ON.DAL.Server
+{
+ public class HttpUtil
+ {
+
+ #region HttpUtil 鍏ㄥ眬甯搁噺
+ ///// <summary>
+ ///// API_HTTPS
+ ///// </summary>
+ //public const string API_HTTPS = "https://";
+ /// <summary>
+ /// RegionMark
+ /// </summary>
+ public const string RegionMark = "HDL";
+ /// <summary>
+ /// 璇锋眰瓒呮椂鏃堕棿
+ /// </summary>
+ public const int TIME_OUT = 10;
+ /// <summary>
+ /// 鐗规畩鎺ュ彛璇锋眰瓒呮椂鏃堕棿
+ /// </summary>
+ public const int TIME_OUT_LONG = 20;
+ /////// <summary>
+ /////// Bearer 鏆傛椂璁句负绌猴紝浠庣櫥闄嗘垚鍔熺殑杩斿洖鐨刪eaderPrefix鍙傛暟鍔ㄦ�佽幏鍙�
+ /////// </summary>
+ //public const string TOKEN_BEARER = "Bearer ";
+
+ #endregion
+
+ /// <summary>
+ /// 鍥哄畾鍩熷悕
+ /// </summary>
+ //public const string GlobalRequestHttpsHost = "https://center.hdlcontrol.com";
+ public const string GlobalRequestHttpsHost = "https://test-gz.hdlcontrol.com";
+
+ /// <summary>
+ /// Get 璇锋眰鏈嶅姟鍣ㄦ柟娉�
+ /// </summary>
+ /// <param name="apiPath"></param>
+ /// <param name="queryDictionary"></param>
+ /// <param name="urlSegmentDictionary"></param>
+ /// <param name="urlHead"></param>
+ /// <param name="replaceToken"></param>
+ /// <param name="mTimeout"></param>
+ /// <returns></returns>
+ public static ResponsePackNew RequestHttpsGet(string apiPath, Dictionary<string, object> queryDictionary = null, Dictionary<string, object> urlSegmentDictionary = null, string urlHead = "", string replaceToken = "", int mTimeout = 10)
+ {
+ return RequestHttps(Method.GET, apiPath, null, queryDictionary, urlSegmentDictionary, urlHead, replaceToken, mTimeout);
+ }
+
+ ///// <summary>
+ ///// 閫氱敤 璇锋眰鏈嶅姟鍣ㄦ柟娉�
+ ///// </summary>
+ ///// <param name="method">璇锋眰绫诲瀷</param>
+ ///// <param name="apiPath"></param>
+ ///// <param name="bodyParameterJson"></param>
+ ///// <param name="queryDictionary"></param>
+ ///// <param name="urlSegmentDictionary"></param>
+ ///// <param name="urlHead"></param>
+ ///// <param name="mTimeout">璇锋眰瓒呮椂鏃堕棿锛岄粯璁�10</param>
+ ///// <param name="urlHead"></param>
+ ///// <returns></returns>
+ //public static ResponsePackNew RequestHttpsBase(Method method, string apiPath, string bodyParameterJson = null, Dictionary<string, object> queryDictionary = null, Dictionary<string, object> urlSegmentDictionary = null, string urlHead = "", string replaceToken = "", int mTimeout = 10, bool needErrorTip = true)
+ //{
+ // var mResponsePackNew = RequestHttps(method, apiPath, bodyParameterJson, queryDictionary, urlSegmentDictionary, urlHead, replaceToken, mTimeout);
+ // if (needErrorTip)
+ // {
+ // if (mResponsePackNew.Code.ToUpper() != SUCCESS_CODE)
+ // {
+ // IMessageCommon.Current.ShowErrorInfoAlter(apiPath, mResponsePackNew.Code);
+ // }
+ // }
+ // return mResponsePackNew;
+ //}
+
+ /// <summary>
+ /// POST璇锋眰鏂规硶 body鍙傛暟
+ /// 閽堝浣忓畢鐩稿叧鎺ュ彛灏佽
+ /// 璋冪敤浣忓畢褰撳墠鎵�鍦ㄥ尯鍩熷煙鍚�
+ /// 濡傛灉鏄垎浜綇瀹咃紝浣跨敤涓讳汉鐨則oken杩涜鐩稿叧鎿嶄綔
+ /// </summary>
+ /// <param name="apiPath"></param>
+ /// <param name="bodyParameterJson"></param>
+ /// <param name="mTimeout"></param>
+ /// <returns></returns>
+ public static ResponsePackNew RequestHttpsPostFroHome(string apiPath, string bodyParameterJson, int mTimeout = 10)
+ {
+ string urlHead = DB_ResidenceData.residenceData.residecenInfo.regionUrl;
+ //var replaceToken = "";
+ //if (DB_ResidenceData.residenceData.residecenInfo.IsOthreShare)
+ //{
+ // replaceToken = DB_ResidenceData.residenceData.MasterToken;
+ //}
+ return RequestHttps(Method.POST, apiPath, bodyParameterJson, null, null, urlHead, "", mTimeout);
+ }
+
+ /// <summary>
+ /// POST璇锋眰鏂规硶 body鍙傛暟
+ /// </summary>
+ /// <param name="apiPath"></param>
+ /// <param name="bodyParameterJson"></param>
+ /// <param name="urlHead"></param>
+ /// <param name="needErrorTip"></param>
+ /// <param name="replaceToken"></param>
+ /// <param name="mTimeout"></param>
+ /// <returns></returns>
+ public static ResponsePackNew RequestHttpsPost(string apiPath, string bodyParameterJson, string urlHead = "", string replaceToken = "", int mTimeout = 10)
+ {
+ return RequestHttps(Method.POST, apiPath, bodyParameterJson, null, null, urlHead, replaceToken, mTimeout);
+ }
+
+ /// <summary>
+ /// POST璇锋眰鏂规硶 queryDictionary
+ /// </summary>
+ /// <param name="apiPath"></param>
+ /// <param name="queryDictionary"></param>
+ /// <param name="urlHead"></param>
+ /// <param name="needErrorTip"></param>
+ /// <param name="replaceToken"></param>
+ /// <param name="mTimeout"></param>
+ /// <returns></returns>
+ public static ResponsePackNew RequestHttpsPost(string apiPath, Dictionary<string, object> queryDictionary, string urlHead = "", string replaceToken = "", int mTimeout = 10)
+ {
+ return RequestHttps(Method.POST, apiPath, null, queryDictionary, null, urlHead, replaceToken, mTimeout);
+ }
+
+ /// <summary>
+ /// 閫氱敤 璇锋眰鏈嶅姟鍣ㄦ柟娉�
+ /// </summary>
+ /// <param name="method"></param>
+ /// <param name="apiPath"></param>
+ /// <param name="bodyParameterJson"></param>
+ /// <param name="queryDictionary"></param>
+ /// <param name="urlSegmentDictionary"></param>
+ /// <param name="urlHead"></param>
+ /// <param name="replaceToken"></param>
+ /// <param name="mTimeout"></param>
+ /// <returns></returns>
+ public static ResponsePackNew RequestHttps(Method method, string apiPath, string bodyParameterJson = null, Dictionary<string, object> queryDictionary = null, Dictionary<string, object> urlSegmentDictionary = null, string urlHead = "", string replaceToken = "", int mTimeout = 10)
+ {
+ #region HttpWebRequest
+ try
+ {
+
+ //if (string.IsNullOrEmpty(urlHead))
+ //{
+ // urlHead = UserInfo.Current.RequestHttpsHost;
+ //}
+ //string requestFullUrl = urlHead + apiPath;
+
+
+ //**************娴嬭瘯**************
+ string requestFullUrl = GlobalRequestHttpsHost + apiPath;
+ //**************娴嬭瘯**************
+
+ RestClient client = new RestClient(requestFullUrl);
+
+ RestRequest request = new RestRequest(method);
+ request.Timeout = mTimeout * 1000;
+ request.AddHeader("content-type", "application/json");
+ //request.AddHeader ("cache-control", "no-cache");
+
+ if (string.IsNullOrEmpty(replaceToken))
+ {
+ if(UserInfo.Current != null)
+ {/* 濡傛灉涓嶉渶瑕侀獙璇乀oken鍙互涓嶇敤浼犲叆 */
+ request.AddHeader("Authorization", UserInfo.Current.LoginTokenString);
+ }
+ }
+ else
+ {
+ request.AddHeader("Authorization", replaceToken);
+ }
+
+ if (bodyParameterJson != null)
+ {
+ request.AddParameter("application/json", bodyParameterJson, ParameterType.RequestBody);
+ }
+
+ if (queryDictionary != null)
+ {
+ foreach (var data in queryDictionary)
+ {
+ request.AddQueryParameter(data.Key, data.Value.ToString());
+ }
+ }
+
+ if (urlSegmentDictionary != null)
+ {
+ foreach (var data in urlSegmentDictionary)
+ {
+ request.AddUrlSegment(data.Key, data.Value.ToString());
+ }
+ }
+
+ IRestResponse response = client.Execute(request);
+ if (response.StatusCode == HttpStatusCode.OK)
+ {
+ try
+ {
+
+ ResponsePackNew revertObj = new ResponsePackNew() { };
+ revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePackNew>(response.Content);
+ //*****涓�浜涘垽绌哄鐞�*****************
+ if (revertObj.Code == null)
+ {
+ revertObj.Code = "DATA_EXCEPTION";
+ }
+
+ if (revertObj.Data == null)
+ {
+ revertObj.Data = "";
+ }
+ //*****涓�浜涘垽绌哄鐞�*****************
+
+ ////缁熶竴杞垚澶у啓
+ //revertObj.StateCode = revertObj.StateCode.ToUpper ();
+ return revertObj;
+ }
+ catch (Exception ex)
+ {
+ HDL_ON.Utlis.WriteLine(ex.Message);
+ return new ResponsePackNew() { Code = "DATA_EXCEPTION" };
+ }
+ }
+ else
+ {
+ HDL_ON.Utlis.WriteLine(response.Content);
+ return new ResponsePackNew() { Code = "NETWORK_ERROR" };
+ }
+
+ }
+ catch (Exception ex)
+ {
+ HDL_ON.Utlis.WriteLine(ex.Message);
+ return new ResponsePackNew() { Code = "NETWORK_ERROR" };
+ }
+
+ #endregion
+
+ }
+
+ /// <summary>
+ /// 涓嬭浇鏂囦欢 璇锋眰鏈嶅姟鍣ㄦ柟娉�
+ /// </summary>
+ /// <param name="apiPath"></param>
+ /// <param name="bodyParameterJson"></param>
+ /// <param name="queryDictionary"></param>
+ /// <param name="urlHead"></param>
+ /// <param name="replaceToken"></param>
+ /// <param name="mTimeout"></param>
+ /// <returns></returns>
+ public static byte[] RequestHttpsDownload(string apiPath, string bodyParameterJson = null, Dictionary<string, object> queryDictionary = null, string urlHead = "", string replaceToken = "", int mTimeout = 20)
+ {
+ #region RestRequest
+ try
+ {
+ //if (string.IsNullOrEmpty (urlHead)) {
+ // urlHead = UserInfo.Current.RequestHttpsHost;
+ //}
+ //string requestFullUrl = urlHead + apiPath;
+
+ //**************娴嬭瘯**************
+ string requestFullUrl = GlobalRequestHttpsHost + apiPath;
+ //**************娴嬭瘯**************
+
+ RestClient client = new RestClient(requestFullUrl);
+ //client.Timeout = mTimeout * 1000;
+ RestRequest request = new RestRequest(Method.POST);
+ request.Timeout = mTimeout * 1000;
+ request.AddHeader("content-type", "application/json");
+
+ if (string.IsNullOrEmpty(replaceToken))
+ {
+ if ( UserInfo.Current != null)
+ {/* 濡傛灉涓嶉渶瑕侀獙璇乀oken鍙互涓嶇敤浼犲叆 */
+ request.AddHeader("Authorization", UserInfo.Current.LoginTokenString);
+ }
+ }
+ else
+ {
+ request.AddHeader("Authorization", replaceToken);
+ }
+
+ if (bodyParameterJson != null)
+ {
+ request.AddParameter("application/json", bodyParameterJson, ParameterType.RequestBody);
+ }
+
+ if (queryDictionary != null)
+ {
+ foreach (var data in queryDictionary)
+ {
+ request.AddQueryParameter(data.Key, data.Value.ToString());
+ }
+ }
+
+ IRestResponse response = client.Execute(request);
+ return response.RawBytes;
+ }
+ catch (Exception ex)
+ {
+ HDL_ON.Utlis.WriteLine(ex.Message);
+ return null;
+ }
+ #endregion
+
+ }
+
+ /// <summary>
+ /// 涓婁紶 璇锋眰鏈嶅姟鍣ㄦ柟娉�
+ /// </summary>
+ /// <param name="method"></param>
+ /// <param name="apiPath"></param>
+ /// <param name="bodyParameterObject"></param>
+ /// <param name="queryDictionary"></param>
+ /// <param name="urlSegmentDictionary"></param>
+ /// <param name="urlHead"></param>
+ /// <param name="replaceToken"></param>
+ /// <param name="mTimeout"></param>
+ /// <returns></returns>
+ public static ResponsePackNew RequestHttpsUpload(Method method, string apiPath, object bodyParameterObject = null, Dictionary<string, object> queryDictionary = null, Dictionary<string, object> urlSegmentDictionary = null, string urlHead = "", string replaceToken = "", int mTimeout = 10)
+ {
+ #region HttpWebRequest
+ try
+ {
+
+ //if (string.IsNullOrEmpty (urlHead)) {
+ // urlHead = UserInfo.Current.RequestHttpsHost;
+ //}
+ //string requestFullUrl = urlHead + apiPath;
+
+ //**************娴嬭瘯**************
+ string requestFullUrl = GlobalRequestHttpsHost + apiPath;
+ //**************娴嬭瘯**************
+
+ RestClient client = new RestClient(requestFullUrl);
+ //client.Timeout = mTimeout * 1000;
+ RestRequest request = new RestRequest(method);
+ request.Timeout = mTimeout * 1000;
+ //request.AddHeader ("content-type", "application/json");
+ request.AddHeader("content-type", "application/octet-stream");
+
+ if (string.IsNullOrEmpty(replaceToken))
+ {
+ if ( UserInfo.Current != null)
+ {/* 濡傛灉涓嶉渶瑕侀獙璇乀oken鍙互涓嶇敤浼犲叆 */
+ request.AddHeader("Authorization", UserInfo.Current.LoginTokenString);
+ }
+ }
+ else
+ {
+ request.AddHeader("Authorization", replaceToken);
+ }
+
+ if (bodyParameterObject != null)
+ {
+ request.AddParameter("application/octet-stream", bodyParameterObject, ParameterType.RequestBody);
+ }
+
+ if (queryDictionary != null)
+ {
+ foreach (var data in queryDictionary)
+ {
+ request.AddQueryParameter(data.Key, data.Value.ToString());
+ }
+ }
+
+ if (urlSegmentDictionary != null)
+ {
+ foreach (var data in urlSegmentDictionary)
+ {
+ request.AddUrlSegment(data.Key, data.Value.ToString());
+ }
+ }
+
+ IRestResponse response = client.Execute(request);
+ if (response.StatusCode == HttpStatusCode.OK)
+ {
+ try
+ {
+ ResponsePackNew revertObj = new ResponsePackNew() { };
+ revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePackNew>(response.Content);
+ if (revertObj.Code == null)
+ {
+ revertObj.Code = "DATA_EXCEPTION";
+ }
+ ////缁熶竴杞垚澶у啓
+ //revertObj.StateCode = revertObj.StateCode.ToUpper ();
+ return revertObj;
+ }
+ catch (Exception ex)
+ {
+ HDL_ON.Utlis.WriteLine(ex.Message);
+ return new ResponsePackNew() { Code = "DATA_EXCEPTION" };
+ }
+ }
+ else
+ {
+ HDL_ON.Utlis.WriteLine(response.Content);
+ return new ResponsePackNew() { Code = "NETWORK_ERROR" };
+ }
+
+ }
+ catch (Exception ex)
+ {
+ HDL_ON.Utlis.WriteLine(ex.Message);
+ return new ResponsePackNew() { Code = "NETWORK_ERROR" };
+ }
+
+ #endregion
+
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ const string APP_KEY = "HDL-HOME-APP-TEST";
+ /// <summary>
+ ///
+ /// </summary>
+ const string SECRET_KEY = "WeJ8TY88vbakCcnvH8G1tDUqzLWY8yss";
+
+ /// <summary>
+ /// 鑾峰彇褰撳墠鏃堕棿鎴冲��
+ /// </summary>
+ /// <returns></returns>
+ static string GetTimestamp()
+ {
+ System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 褰撳湴鏃跺尯
+ return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 鐩稿樊绉掓暟
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="signstr"></param>
+ /// <returns></returns>
+ static string SignMD5Encrypt(string s)
+ {
+ byte[] sign = MD5.Create().ComputeHash(UTF8Encoding.UTF8.GetBytes(s));
+ string signstr = string.Empty;
+ foreach (byte item in sign)
+ {
+ signstr += item.ToString("X2");
+ }
+ return signstr.ToLower();
+ }
+
+ /// <summary>
+ /// 2020-11-02
+ /// 鍩虹鏈嶅姟鐨勬帴鍙i兘瑕佹牎楠宻ign
+ /// 璁$畻sign绛惧悕
+ /// </summary>
+ /// <returns></returns>
+ public static string GetSignRequestJson(object requestObj)
+ {
+ try
+ {
+ //1. 灏唌odel瀹炰綋杞负Dictionary<string, object>
+ var paramDictionary = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(Newtonsoft.Json.JsonConvert.SerializeObject(requestObj));
+ //2. 璁$畻sign
+ if (paramDictionary != null)
+ {
+ paramDictionary.Add("appKey", APP_KEY);
+ paramDictionary.Add("timestamp", GetTimestamp());
+ //2.1 瀛楀吀鍗囧簭
+ paramDictionary = paramDictionary.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
+ //2.2 鎷兼帴鎸塙RL閿�煎
+ string str = string.Empty;
+ foreach (KeyValuePair<string, object> item in paramDictionary)
+ {
+ //Value涓簄ull涓嶅弬鍔犳牎楠�
+ if (item.Value != null)
+ {
+ //Value.ToString()涓簄ull鎴栬��""涔熶笉鍙傚姞鏍¢獙
+ if (!string.IsNullOrEmpty(item.Value.ToString()))
+ {
+ //濡傛灉鏄痓ool绫诲瀷锛岃杞皬鍐�
+ if (item.Value is bool)
+ {
+ str += item.Key + "=" + item.Value.ToString().ToLower() + "&";
+ }
+ else
+ {
+ str += item.Key + "=" + item.Value.ToString() + "&";
+ }
+ }
+ }
+ }
+
+ //2.3 鎷兼帴SECRET_KEY
+ str = str.Substring(0, str.Length - 1) + SECRET_KEY;
+ //2.4 MD5杞崲+杞皬鍐�
+ var signstr = SignMD5Encrypt(str);
+ paramDictionary.Add("sign", signstr);
+ return Newtonsoft.Json.JsonConvert.SerializeObject(paramDictionary);
+ }
+ else
+ {
+ return "";
+ }
+ }
+ catch
+ {
+ return "";
+ }
+ }
+
+ }
+
+ /// <summary>
+ /// 鍝嶅簲鍙傛暟
+ /// </summary>
+ [Serializable]
+ public class ResponsePackNew
+ {
+ /// <summary>
+ /// 鍝嶅簲鐘舵�佺爜
+ /// </summary>
+ public string Code;
+
+ /// <summary>
+ /// 鍝嶅簲鍐呭
+ /// </summary>
+ public object Data;
+
+ /// <summary>
+ /// 鍝嶅簲閿欒淇℃伅
+ /// </summary>
+ public string message;
+
+ ///// <summary>
+ ///// isSuccess
+ ///// </summary>
+ //public bool isSuccess;
+
+ ///// <summary>
+ ///// timestamp
+ ///// </summary>
+ //public string timestamp;
+
+
+ }
+
+
+
+}
--
Gitblit v1.8.0