| | |
| | | public class HttpUtil |
| | | { |
| | | |
| | | #region HttpUtil 全局常量 |
| | | #region **********全局常量********** |
| | | ///// <summary> |
| | | ///// API_HTTPS |
| | | ///// </summary> |
| | |
| | | |
| | | #endregion |
| | | |
| | | #region **********网络请求封装********** |
| | | /// <summary> |
| | | /// 固定域名,正式环境 |
| | | /// </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) |
| | | { |
| | | #region HttpWebRequest |
| | | try |
| | |
| | | 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)) |
| | | { |
| | |
| | | { |
| | | try |
| | | { |
| | | |
| | | ResponsePackNew revertObj = new ResponsePackNew() { }; |
| | | revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePackNew>(response.Content); |
| | | //*****一些判空处理***************** |
| | |
| | | } |
| | | //*****一些判空处理***************** |
| | | |
| | | ////统一转成大写 |
| | | //revertObj.StateCode = revertObj.StateCode.ToUpper (); |
| | | return revertObj; |
| | | } |
| | | catch (Exception ex) |
| | |
| | | |
| | | #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> |
| | |
| | | ////**************测试************** |
| | | |
| | | 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"); |
| | |
| | | |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region **********签名校验********** |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | } |