| | |
| | | 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) |
| | | { |
| | | #region HttpWebRequest |
| | | try |
| | | { |
| | | try { |
| | | |
| | | if (string.IsNullOrEmpty(urlHead)) |
| | | { |
| | | if (string.IsNullOrEmpty (urlHead)) { |
| | | urlHead = GlobalRequestHttpsHost; |
| | | } |
| | | string requestFullUrl = urlHead + apiPath; |
| | |
| | | request.Timeout = mTimeout * 1000; |
| | | request.AddHeader("content-type", "application/json"); |
| | | |
| | | if (string.IsNullOrEmpty(replaceToken)) |
| | | { |
| | | if(MainPage.LoginUser != null) |
| | | {/* 如果不需要验证Token可以不用传入 */ |
| | | if (string.IsNullOrEmpty (replaceToken)) { |
| | | if (MainPage.LoginUser != null) {/* 如果不需要验证Token可以不用传入 */ |
| | | request.AddHeader("Authorization", MainPage.LoginUser.LoginTokenString); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | request.AddHeader("Authorization", replaceToken); |
| | | } |
| | | |
| | | if (bodyParameterJson != null) |
| | | { |
| | | if (bodyParameterJson != null) { |
| | | request.AddParameter("application/json", bodyParameterJson, ParameterType.RequestBody); |
| | | } |
| | | |
| | | if (queryDictionary != null) |
| | | { |
| | | foreach (var data in queryDictionary) |
| | | { |
| | | if (queryDictionary != null) { |
| | | foreach (var data in queryDictionary) { |
| | | request.AddQueryParameter(data.Key, data.Value.ToString()); |
| | | } |
| | | } |
| | | |
| | | if (urlSegmentDictionary != null) |
| | | { |
| | | foreach (var data in urlSegmentDictionary) |
| | | { |
| | | 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 |
| | | { |
| | | if (response.StatusCode == HttpStatusCode.OK) { |
| | | try { |
| | | ResponsePackNew revertObj = new ResponsePackNew() { }; |
| | | revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePackNew>(response.Content); |
| | | //*****一些判空处理***************** |
| | | if (revertObj.Code == null) |
| | | { |
| | | if (revertObj.Code == null) { |
| | | revertObj.Code = StateCode.DATA_EXCEPTION; |
| | | } |
| | | |
| | | if (revertObj.Data == null) |
| | | { |
| | | if (revertObj.Data == null) { |
| | | revertObj.Data = ""; |
| | | } |
| | | //*****一些判空处理***************** |
| | | |
| | | return revertObj; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | } catch (Exception ex) { |
| | | Utlis.WriteLine(ex.Message); |
| | | return new ResponsePackNew() { Code = StateCode.DATA_EXCEPTION }; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | Utlis.WriteLine(response.Content); |
| | | return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | } catch (Exception ex) { |
| | | Utlis.WriteLine(ex.Message); |
| | | return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; |
| | | } |
| | |
| | | { |
| | | var responsePackNew = RequestHttpsBase(method, apiPath, bodyParameterJson, queryDictionary, urlSegmentDictionary, urlHead, replaceToken, mTimeout); |
| | | //*****************Token过期处理***************** |
| | | if (responsePackNew.Code == StateCode.TOKEN_EXPIRED) |
| | | { |
| | | if (responsePackNew.Code == StateCode.TOKEN_EXPIRED) { |
| | | //刷新Token |
| | | if (RefreshToken()) { |
| | | return RequestHttpsBase(method, apiPath, bodyParameterJson, queryDictionary, urlSegmentDictionary, urlHead, replaceToken, mTimeout); |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | return responsePackNew; |
| | | } |
| | | } |
| | |
| | | /// <returns></returns> |
| | | static bool RefreshToken() |
| | | { |
| | | try |
| | | { |
| | | var requestJson = GetSignRequestJson(new RefreshTokenObj() |
| | | { |
| | | try { |
| | | var requestJson = GetSignRequestJson (new RefreshTokenObj () { |
| | | refreshToken = MainPage.LoginUser.RefreshToken, |
| | | }); |
| | | var revertObj = RequestHttpsBase(Method.POST, NewAPI.API_POST_Login, requestJson); |
| | | if (revertObj.Code == StateCode.SUCCESS) |
| | | { |
| | | if (revertObj.Code == StateCode.SUCCESS) { |
| | | var revertData = Newtonsoft.Json.JsonConvert.DeserializeObject<UserLoginRes>(revertObj.Data.ToString()); |
| | | MainPage.LoginUser.LoginTokenString = revertData.headerPrefix + revertData.accessToken; |
| | | MainPage.LoginUser.RefreshToken = revertData.refreshToken; |
| | |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | catch |
| | | { |
| | | } catch { |
| | | return false; |
| | | } |
| | | } |
| | |
| | | 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)) |
| | | { |
| | | try { |
| | | if (string.IsNullOrEmpty (urlHead)) { |
| | | urlHead = GlobalRequestHttpsHost; |
| | | } |
| | | string requestFullUrl = urlHead + apiPath; |
| | |
| | | request.Timeout = mTimeout * 1000; |
| | | request.AddHeader("content-type", "application/json"); |
| | | |
| | | if (string.IsNullOrEmpty(replaceToken)) |
| | | { |
| | | if ( MainPage.LoginUser != null) |
| | | {/* 如果不需要验证Token可以不用传入 */ |
| | | if (string.IsNullOrEmpty (replaceToken)) { |
| | | if (MainPage.LoginUser != null) {/* 如果不需要验证Token可以不用传入 */ |
| | | request.AddHeader("Authorization", MainPage.LoginUser.LoginTokenString); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | request.AddHeader("Authorization", replaceToken); |
| | | } |
| | | |
| | | if (bodyParameterJson != null) |
| | | { |
| | | if (bodyParameterJson != null) { |
| | | request.AddParameter("application/json", bodyParameterJson, ParameterType.RequestBody); |
| | | } |
| | | |
| | | if (queryDictionary != null) |
| | | { |
| | | foreach (var data in queryDictionary) |
| | | { |
| | | 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) |
| | | { |
| | | } catch (Exception ex) { |
| | | Utlis.WriteLine(ex.Message); |
| | | return null; |
| | | } |
| | |
| | | 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 = 20) |
| | | { |
| | | #region HttpWebRequest |
| | | try |
| | | { |
| | | try { |
| | | |
| | | if (string.IsNullOrEmpty(urlHead)) |
| | | { |
| | | if (string.IsNullOrEmpty (urlHead)) { |
| | | urlHead = GlobalRequestHttpsHost; |
| | | } |
| | | string requestFullUrl = urlHead + apiPath; |
| | |
| | | //request.AddHeader ("content-type", "application/json"); |
| | | request.AddHeader("content-type", "application/octet-stream"); |
| | | |
| | | if (string.IsNullOrEmpty(replaceToken)) |
| | | { |
| | | if ( MainPage.LoginUser != null) |
| | | {/* 如果不需要验证Token可以不用传入 */ |
| | | if (string.IsNullOrEmpty (replaceToken)) { |
| | | if (MainPage.LoginUser != null) {/* 如果不需要验证Token可以不用传入 */ |
| | | request.AddHeader("Authorization", MainPage.LoginUser.LoginTokenString); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | request.AddHeader("Authorization", replaceToken); |
| | | } |
| | | |
| | | if (bodyParameterObject != null) |
| | | { |
| | | if (bodyParameterObject != null) { |
| | | request.AddParameter("application/octet-stream", bodyParameterObject, ParameterType.RequestBody); |
| | | } |
| | | |
| | | if (queryDictionary != null) |
| | | { |
| | | foreach (var data in queryDictionary) |
| | | { |
| | | if (queryDictionary != null) { |
| | | foreach (var data in queryDictionary) { |
| | | request.AddQueryParameter(data.Key, data.Value.ToString()); |
| | | } |
| | | } |
| | | |
| | | if (urlSegmentDictionary != null) |
| | | { |
| | | foreach (var data in urlSegmentDictionary) |
| | | { |
| | | 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 |
| | | { |
| | | if (response.StatusCode == HttpStatusCode.OK) { |
| | | try { |
| | | ResponsePackNew revertObj = new ResponsePackNew() { }; |
| | | revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePackNew>(response.Content); |
| | | if (revertObj.Code == null) |
| | | { |
| | | if (revertObj.Code == null) { |
| | | revertObj.Code = StateCode.DATA_EXCEPTION; |
| | | } |
| | | ////统一转成大写 |
| | | //revertObj.StateCode = revertObj.StateCode.ToUpper (); |
| | | return revertObj; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | } catch (Exception ex) { |
| | | Utlis.WriteLine(ex.Message); |
| | | return new ResponsePackNew() { Code = StateCode.DATA_EXCEPTION }; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | Utlis.WriteLine(response.Content); |
| | | return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | } catch (Exception ex) { |
| | | Utlis.WriteLine(ex.Message); |
| | | return new ResponsePackNew() { Code = StateCode.NETWORK_ERROR }; |
| | | } |
| | |
| | | public static byte[] HttpsDownload(string requestFullUrl, int mTimeout = 30) |
| | | { |
| | | #region RestRequest |
| | | try |
| | | { |
| | | 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) |
| | | { |
| | | } catch (Exception ex) { |
| | | Utlis.WriteLine(ex.Message); |
| | | return null; |
| | | } |
| | |
| | | { |
| | | byte[] sign = MD5.Create().ComputeHash(UTF8Encoding.UTF8.GetBytes(s)); |
| | | string signstr = string.Empty; |
| | | foreach (byte item in sign) |
| | | { |
| | | 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> |
| | |
| | | /// <returns></returns> |
| | | public static string GetSignRequestJson(object requestObj) |
| | | { |
| | | try |
| | | { |
| | | try { |
| | | //1. 将model实体转为Dictionary<string, object> |
| | | var paramDictionary = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(Newtonsoft.Json.JsonConvert.SerializeObject(requestObj)); |
| | | //2. 计算sign |
| | | if (paramDictionary != null) |
| | | { |
| | | 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 拼接按URL键值对 |
| | | string str = string.Empty; |
| | | foreach (KeyValuePair<string, object> item in paramDictionary) |
| | | { |
| | | foreach (KeyValuePair<string, object> item in paramDictionary) { |
| | | //Value为null不参加校验 |
| | | if (item.Value != null) |
| | | { |
| | | //Value.ToString()为null或者""也不参加校验 |
| | | if (!string.IsNullOrEmpty(item.Value.ToString())) |
| | | { |
| | | if (item.Value != null) { |
| | | //检测当前参数是否需要参与校验 |
| | | if (IfValueNeedSign (item.Value.ToString ())) { |
| | | //如果是bool类型,要转小写 |
| | | if (item.Value is bool) |
| | | { |
| | | if (item.Value is bool) { |
| | | str += item.Key + "=" + item.Value.ToString().ToLower() + "&"; |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | str += item.Key + "=" + item.Value.ToString() + "&"; |
| | | } |
| | | } |
| | |
| | | paramDictionary.Add("sign", signstr); |
| | | var signResult = Newtonsoft.Json.JsonConvert.SerializeObject(paramDictionary); |
| | | return signResult; |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | return ""; |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | } catch { |
| | | return ""; |
| | | } |
| | | } |