/// <summary> /// 请求 /// </summary> /// <param name="httpInvoke"></param> /// <param name="processError">异常处理,会抛出异常</param> /// <param name="processSuccess">成功处理</param> /// <param name="loadArgs">Http基础请求添加额外字段</param> /// <returns></returns> public static T Request <T>(this HttpInvoke httpInvoke, Action <Result> processError, Action <T> processSuccess = null, Func <Dictionary <string, string> > loadArgs = null) where T : class { var result = Run(httpInvoke, loadArgs); if (result.Wrong == Wrong.UnLogin || result.Wrong == Wrong.LoginTimeOut) { var token = GetLogin().Login(httpInvoke.AppID); SetToken(token); } if (result.IsSuccess) { var t = _jsonConvert.DeserializeObject <T>(result.Data); if (t != null) { processSuccess?.Invoke(t); } else { result.IsSuccess = false; result.Wrong = Wrong.JsonDeserializeNullError; processError(result); } return(t); } else { processError(result); } return(null); }
/// <summary> /// 请求 /// </summary> /// <param name="httpInvoke"></param> /// <param name="loadArgs">Http基础请求添加额外字段</param> /// <returns></returns> public static Result Request(this HttpInvoke httpInvoke, Func <Dictionary <string, string> > loadArgs = null) { var result = Run(httpInvoke, loadArgs); if (result.Wrong == Wrong.UnLogin || result.Wrong == Wrong.LoginTimeOut) { result = Run(httpInvoke, loadArgs); } return(result); }
private static Result Run(HttpInvoke httpInvoke, Func <Dictionary <string, string> > loadArgs = null) { Init(); if (httpInvoke == null) { throw new Exception("请求参数不正确"); } var postdata = _jsonConvert.SerializeObject(new { AppID = httpInvoke.AppID, Method = httpInvoke.Method, RequestObjs = httpInvoke.RequestObjs }); Dictionary <string, string> dic; if (loadArgs != null) { dic = loadArgs() ?? new Dictionary <string, string>(); } else { dic = new Dictionary <string, string>(); } #region loadArgs dic.Add("hversion", httpInvoke.Version); dic.Add("channel", GetPath()); var _login = _logins[_3rdHttp]; if (_token != null && _login.NeedLogin(httpInvoke.AppID)) { if (_login.IsLocalToken(httpInvoke.AppID)) { dic.Add("uid", _login.UID); dic.Add("token", _login.Token); } else { dic.Add("uid", _token.UID); dic.Add("token", _token.Token); } } dic.Add("cip", ip); dic.Add("sendertime", httpInvoke.SenderTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); if (!string.IsNullOrWhiteSpace(httpInvoke.ID)) { dic.Add("requestid", httpInvoke.ID); } if (httpInvoke.ExecuteID != Guid.Empty) { dic.Add("executeid", httpInvoke.ExecuteID.ToString()); } #endregion var ret = HttpHelper.PostRequest(_3rdHttp + "Http/CHttpRequest", postdata, dic, _timeout); if (ret.IsSuccess == false) { var error = ret.GetWrong(); if (error == Wrong.UnLogin || error == Wrong.LoginTimeOut) { SetToken(GetLogin().Login(httpInvoke.AppID)); } if (httpInvoke.CanRetry && httpInvoke.RetryTimes > 0) { for (int i = 0; i < httpInvoke.RetryTimes; i++) { Console.WriteLine("重试{0}次", i + 1); ret = HttpHelper.PostRequest(_3rdHttp + "Http/CHttpRequest", postdata, dic, _timeout); if (ret.IsSuccess) { break; } } } } return(new Result() { Data = ret.d, IsSuccess = ret.IsSuccess, HttpCode = ret.code, Wrong = ret.GetWrong(), }); }