| | |
| | | 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://"; |
| | | #region **********全局常量********** |
| | | /// <summary> |
| | | /// 固定域名,正式环境 |
| | | /// 公共域名就近解析 |
| | | /// |
| | | /// </summary> |
| | | //public const string GlobalRequestHttpsHost = "https://nearest.hdlcontrol.com"; |
| | | public const string GlobalRequestHttpsHost = "https://test-gz.hdlcontrol.com";//mmmm |
| | | /// <summary> |
| | | /// RegionMark |
| | | /// </summary> |
| | |
| | | |
| | | #endregion |
| | | |
| | | /// <summary> |
| | | /// 固定域名 |
| | | /// </summary> |
| | | //public const string GlobalRequestHttpsHost = "https://center.hdlcontrol.com"; |
| | | public const string GlobalRequestHttpsHost = "https://test-gz.hdlcontrol.com"; |
| | | #region **********网络请求封装********** |
| | | |
| | | /// <summary> |
| | | /// Get 请求服务器方法 |
| | |
| | | /// <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, replaceToken, mTimeout); |
| | | 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> |
| | |
| | | /// <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) |
| | | 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 = UserInfo.Current.RequestHttpsHost; |
| | | //} |
| | | //string requestFullUrl = urlHead + apiPath; |
| | | if (string.IsNullOrEmpty(urlHead)) |
| | | { |
| | | urlHead = OnAppConfig.Instance.RequestHttpsHost; |
| | | } |
| | | string requestFullUrl = urlHead + apiPath; |
| | | |
| | | |
| | | //**************测试************** |
| | | string requestFullUrl = GlobalRequestHttpsHost + 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 (revertObj.Code == null) |
| | | { |
| | | revertObj.Code = "DATA_EXCEPTION"; |
| | | revertObj.Code = StateCode.DATA_EXCEPTION; |
| | | } |
| | | |
| | | if (revertObj.Data == null) |
| | |
| | | } |
| | | //*****一些判空处理***************** |
| | | |
| | | ////统一转成大写 |
| | | //revertObj.StateCode = revertObj.StateCode.ToUpper (); |
| | | return revertObj; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | HDL_ON.Utlis.WriteLine(ex.Message); |
| | | return new ResponsePackNew() { Code = "DATA_EXCEPTION" }; |
| | | return new ResponsePackNew() { Code = StateCode.DATA_EXCEPTION }; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | HDL_ON.Utlis.WriteLine(response.Content); |
| | | return new ResponsePackNew() { Code = "NETWORK_ERROR" }; |
| | | return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | HDL_ON.Utlis.WriteLine(ex.Message); |
| | | return new ResponsePackNew() { Code = "NETWORK_ERROR" }; |
| | | 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> |
| | |
| | | #region RestRequest |
| | | try |
| | | { |
| | | //if (string.IsNullOrEmpty (urlHead)) { |
| | | // urlHead = UserInfo.Current.RequestHttpsHost; |
| | | //} |
| | | //string requestFullUrl = urlHead + apiPath; |
| | | if (string.IsNullOrEmpty(urlHead)) |
| | | { |
| | | urlHead = OnAppConfig.Instance.RequestHttpsHost; |
| | | } |
| | | string requestFullUrl = urlHead + apiPath; |
| | | |
| | | //**************测试************** |
| | | string requestFullUrl = GlobalRequestHttpsHost + 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"); |
| | |
| | | try |
| | | { |
| | | |
| | | //if (string.IsNullOrEmpty (urlHead)) { |
| | | // urlHead = UserInfo.Current.RequestHttpsHost; |
| | | //} |
| | | //string requestFullUrl = urlHead + apiPath; |
| | | if (string.IsNullOrEmpty(urlHead)) |
| | | { |
| | | urlHead = OnAppConfig.Instance.RequestHttpsHost; |
| | | } |
| | | string requestFullUrl = urlHead + apiPath; |
| | | |
| | | //**************测试************** |
| | | string requestFullUrl = GlobalRequestHttpsHost + apiPath; |
| | | //**************测试************** |
| | | ////**************测试************** |
| | | //string requestFullUrl = GlobalRequestHttpsHost + apiPath; |
| | | ////**************测试************** |
| | | |
| | | RestClient client = new RestClient(requestFullUrl); |
| | | //client.Timeout = mTimeout * 1000; |
| | |
| | | revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePackNew>(response.Content); |
| | | if (revertObj.Code == null) |
| | | { |
| | | revertObj.Code = "DATA_EXCEPTION"; |
| | | revertObj.Code = StateCode.DATA_EXCEPTION; |
| | | } |
| | | ////统一转成大写 |
| | | //revertObj.StateCode = revertObj.StateCode.ToUpper (); |
| | |
| | | catch (Exception ex) |
| | | { |
| | | HDL_ON.Utlis.WriteLine(ex.Message); |
| | | return new ResponsePackNew() { Code = "DATA_EXCEPTION" }; |
| | | return new ResponsePackNew() { Code = StateCode.DATA_EXCEPTION }; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | HDL_ON.Utlis.WriteLine(response.Content); |
| | | return new ResponsePackNew() { Code = "NETWORK_ERROR" }; |
| | | return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | HDL_ON.Utlis.WriteLine(ex.Message); |
| | | return new ResponsePackNew() { Code = "NETWORK_ERROR" }; |
| | | 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> |
| | |
| | | static string GetTimestamp() |
| | | { |
| | | System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 |
| | | return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 相差秒数 |
| | | return ((long)(DateTime.Now - startTime).TotalMilliseconds).ToString(); // 相差秒数 |
| | | //return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 相差秒数 |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | } |
| | | 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 |
| | | /// 基础服务的接口都要校验sign |
| | | /// 计算sign签名 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static string GetSignRequestJson(object requestObj) |
| | | public static string GetSignRequestJson(object requestObj,Dictionary<string,object> paramDictionary = null) |
| | | { |
| | | try |
| | | { |
| | | //1. 将model实体转为Dictionary<string, object> |
| | | var paramDictionary = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(Newtonsoft.Json.JsonConvert.SerializeObject(requestObj)); |
| | | if (paramDictionary == null) |
| | | { |
| | | paramDictionary = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(Newtonsoft.Json.JsonConvert.SerializeObject(requestObj)); |
| | | } |
| | | //2. 计算sign |
| | | if (paramDictionary != null) |
| | | { |
| | |
| | | if (item.Value != null) |
| | | { |
| | | //Value.ToString()为null或者""也不参加校验 |
| | | if (!string.IsNullOrEmpty(item.Value.ToString())) |
| | | //if (!string.IsNullOrEmpty(item.Value.ToString()) && (item.Value is string || item.Value.GetType().IsValueType)) |
| | | //{ |
| | | //检测当前参数是否需要参与校验 |
| | | if (IfValueNeedSign(item.Value.ToString())) |
| | | { |
| | | //如果是bool类型,要转小写 |
| | | if (item.Value is bool) |
| | |
| | | //2.4 MD5转换+转小写 |
| | | var signstr = SignMD5Encrypt(str); |
| | | paramDictionary.Add("sign", signstr); |
| | | return Newtonsoft.Json.JsonConvert.SerializeObject(paramDictionary); |
| | | var signResult = Newtonsoft.Json.JsonConvert.SerializeObject(paramDictionary); |
| | | return signResult; |
| | | } |
| | | else |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | public string message; |
| | | |
| | | /// <summary> |
| | | /// 这个是请求错误时的扩展数据,以后所有的附带扩展数据都会放在这里面动态维护 |
| | | /// </summary> |
| | | public object extra; |
| | | |
| | | ///// <summary> |
| | | ///// isSuccess |
| | | ///// </summary> |
| | |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | } |