From eda3fb873e59544ff36301b51e05aef64f87b0f9 Mon Sep 17 00:00:00 2001 From: wei <kaede@kaededeMacBook-Air.local> Date: 星期五, 27 八月 2021 13:21:21 +0800 Subject: [PATCH] Merge branch 'newBranch1' of http://172.16.1.23:6688/r/~wxr/HDL_APP_Project into newBranch1 --- HDL_ON/DAL/Server/HttpUtil.cs | 673 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 673 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..48d5724 --- /dev/null +++ b/HDL_ON/DAL/Server/HttpUtil.cs @@ -0,0 +1,673 @@ +锘縰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; + +namespace HDL_ON.DAL.Server +{ + public class HttpUtil + { + + #region **********鍏ㄥ眬甯搁噺********** + /// <summary> + /// 鍥哄畾鍩熷悕,姝e紡鐜 + /// 鍏叡鍩熷悕灏辫繎瑙f瀽 + /// </summary> + //public const string GlobalRequestHttpsHost = "https://nearest.hdlcontrol.com"; + public const string GlobalRequestHttpsHost = "https://test-gz.hdlcontrol.com";//mmmm + /// <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 + + #region **********缃戠粶璇锋眰灏佽********** + + /// <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.Instance.CurrentRegion.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> + 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) + { + //Dome妯″紡鐧诲綍 + + if(MainPage.NoLoginMode) + { + return new ResponsePackNew() { Code = "0" }; + } + #region HttpWebRequest + try + { + + if (string.IsNullOrEmpty(urlHead)) + { + urlHead = OnAppConfig.Instance.RequestHttpsHost; + } + string requestFullUrl = urlHead + apiPath; + + + RestClient client = new RestClient(requestFullUrl); + + RestRequest request = new RestRequest(method); + 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()); + } + } + + 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 = StateCode.DATA_EXCEPTION; + } + + if (revertObj.Data == null) + { + revertObj.Data = ""; + } + //*****涓�浜涘垽绌哄鐞�***************** + + return revertObj; + } + catch (Exception ex) + { + HDL_ON.Utlis.WriteLine(ex.Message); + return new ResponsePackNew() { Code = StateCode.DATA_EXCEPTION }; + } + } + else + { + HDL_ON.Utlis.WriteLine(response.Content); + return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; + } + + } + catch (Exception ex) + { + HDL_ON.Utlis.WriteLine(ex.Message); + return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; + } + + #endregion + + } + + /// <summary> + /// 閫氱敤 璇锋眰鏈嶅姟鍣ㄦ柟娉� + /// 澧炲姞token杩囨湡澶勭悊 + /// </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) + { + var responsePackNew = RequestHttpsBase(method, apiPath, bodyParameterJson, queryDictionary, urlSegmentDictionary, urlHead, replaceToken, mTimeout); + //*****************Token杩囨湡澶勭悊***************** + if (responsePackNew.Code == StateCode.TOKEN_EXPIRED) + { + //鍒锋柊Token + if (RefreshToken()) { + return RequestHttpsBase(method, apiPath, bodyParameterJson, queryDictionary, urlSegmentDictionary, urlHead, replaceToken, mTimeout); + } + else + { + return responsePackNew; + } + } + //*****************Token杩囨湡澶勭悊***************** + return responsePackNew; + } + + + /// <summary> + /// 鍒锋柊 Token + /// </summary> + /// <returns></returns> + static bool RefreshToken() + { + try + { + var requestJson = GetSignRequestJson(new RefreshTokenObj() + { + refreshToken = UserInfo.Current.RefreshToken, + }); + var revertObj = RequestHttpsBase(Method.POST, NewAPI.API_POST_Login, requestJson); + if (revertObj.Code == StateCode.SUCCESS) + { + var revertData = Newtonsoft.Json.JsonConvert.DeserializeObject<UserLoginRes>(revertObj.Data.ToString()); + UserInfo.Current.LoginTokenString = revertData.headerPrefix + revertData.accessToken; + UserInfo.Current.RefreshToken = revertData.refreshToken; + UserInfo.Current.LastTime = DateTime.Now; + UserInfo.Current.SaveUserInfo(); + return true; + } + return false; + } + catch + { + return false; + } + } + + /// <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 = OnAppConfig.Instance.RequestHttpsHost; + } + string requestFullUrl = urlHead + apiPath; + + RestClient client = new RestClient(requestFullUrl); + 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 = OnAppConfig.Instance.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 = StateCode.DATA_EXCEPTION; + } + ////缁熶竴杞垚澶у啓 + //revertObj.StateCode = revertObj.StateCode.ToUpper (); + return revertObj; + } + catch (Exception ex) + { + HDL_ON.Utlis.WriteLine(ex.Message); + return new ResponsePackNew() { Code = StateCode.DATA_EXCEPTION }; + } + } + else + { + HDL_ON.Utlis.WriteLine(response.Content); + return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; + } + + } + catch (Exception ex) + { + HDL_ON.Utlis.WriteLine(ex.Message); + return new ResponsePackNew() { Code = StateCode.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[] HttpsDownload(string requestFullUrl, int mTimeout = 30) + { + #region RestRequest + try + { + RestClient client = new RestClient(requestFullUrl); + RestRequest request = new RestRequest(Method.GET); + request.Timeout = mTimeout * 1000; + + IRestResponse response = client.Execute(request); + return response.RawBytes; + } + catch (Exception ex) + { + HDL_ON.Utlis.WriteLine(ex.Message); + return null; + } + #endregion + + } + + #endregion + + #region **********绛惧悕鏍¢獙********** + /// <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).TotalMilliseconds).ToString(); // 鐩稿樊绉掓暟 + //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> + /// 鍒ゆ柇褰撳墠鍊兼槸鍚﹂渶瑕佸弬涓庣鍚嶏紝淇濇寔璺熶簯绔竴鑷� + /// 绌哄瓧绗︿覆涓嶅弬涓� + /// 鏁扮粍,闆嗗悎,瀵硅薄涓嶅弬涓� + /// </summary> + /// <param name="valueStr"></param> + /// <returns></returns> + static bool IfValueNeedSign(string valueStr) + { + if (string.IsNullOrEmpty(valueStr) || valueStr.StartsWith("{") || valueStr.StartsWith("[")) + { + return false; + } + return true; + } + /// <summary> + /// 2020-11-02 + /// 鍩虹鏈嶅姟鐨勬帴鍙i兘瑕佹牎楠宻ign + /// 璁$畻sign绛惧悕 + /// </summary> + /// <returns></returns> + public static string GetSignRequestJson(object requestObj,Dictionary<string,object> paramDictionary = null) + { + try + { + //1. 灏唌odel瀹炰綋杞负Dictionary<string, object> + if (paramDictionary == null) + { + 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()) && (item.Value is string || item.Value.GetType().IsValueType)) + //{ + //妫�娴嬪綋鍓嶅弬鏁版槸鍚﹂渶瑕佸弬涓庢牎楠� + if (IfValueNeedSign(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); + var signResult = Newtonsoft.Json.JsonConvert.SerializeObject(paramDictionary); + return signResult; + } + else + { + return ""; + } + } + catch + { + return ""; + } + } + + #endregion + } + + /// <summary> + /// 鍝嶅簲鍙傛暟 + /// </summary> + [Serializable] + public class ResponsePackNew + { + /// <summary> + /// 鍝嶅簲鐘舵�佺爜 + /// </summary> + public string Code; + + /// <summary> + /// 鍝嶅簲鍐呭 + /// </summary> + public object Data; + + /// <summary> + /// 鍝嶅簲閿欒淇℃伅 + /// </summary> + public string message = ""; + + /// <summary> + /// 杩欎釜鏄姹傞敊璇椂鐨勬墿灞曟暟鎹�,浠ュ悗鎵�鏈夌殑闄勫甫鎵╁睍鏁版嵁閮戒細鏀惧湪杩欓噷闈㈠姩鎬佺淮鎶� + /// </summary> + public object extra; + + ///// <summary> + ///// isSuccess + ///// </summary> + //public bool isSuccess; + + ///// <summary> + ///// timestamp + ///// </summary> + //public string timestamp; + + + } + +} -- Gitblit v1.8.0