/// <summary> /// 带重试次数的post /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="json"></param> /// <param name="tryTimes"></param> /// <param name="headers"></param> /// <param name="timeOut"></param> /// <returns></returns> public static T TryPost <T>(string url, string json, int tryTimes = 10, WebHeaderCollection headers = null, int timeOut = 120 * 1000) { return(SerializeHelper.Deserialize <T>(TryPost(url, json, tryTimes, headers, timeOut))); }
/// <summary> /// 外部请求跟踪日志方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="get"></param> /// <param name="func"></param> /// <param name="params"></param> /// <returns>logcom 第一个参数为url,最后一个参数是返回值</returns> public static T TackingLog <T>(bool get, Func <T> func, params object[] @params) { if (@params == null) { return(default(T)); } var list = new List <object>(); list.AddRange(@params); try { StackTrace st = new StackTrace(); var sfs = st.GetFrames(); for (int i = 1; i < sfs.Length; ++i) { if (StackFrame.OFFSET_UNKNOWN == sfs[i].GetILOffset()) { break; } var methodName = sfs[i].GetMethod().Name; var fullName = sfs[i].GetFileName() + "()->" + methodName; list.Add(fullName); } } catch { } T result = default(T); var mil = 0L; Stopwatch stopwatch = Stopwatch.StartNew(); try { result = func.Invoke(); mil = stopwatch.ElapsedMilliseconds; list.Add("cost:" + mil); list.Add(mil); list.Add(result); } catch (Exception ex) { mil = stopwatch.ElapsedMilliseconds; list.Add("cost:" + mil); list.Add(mil); list.Add("err:" + ex.Message); if (ex.Message.IndexOf("操作超时") > -1 || ex.Message.IndexOf("Timeout") > -1) { OnLogged?.Invoke(new APILog(level: 2, operationName: "调用五秒超时日志", des: get ? "ApiHelper.Get" : "ApiHelper.Post", url: @params[0].ToString(), result: "", cost: (int)mil, method: (get ? "GET" : "POST"), mode: "Active", exp: ex, @params: list.ToArray())); } else { OnLogged?.Invoke(new APILog(level: 3, operationName: "调用异常日志", des: get ? "ApiHelper.Get" : "ApiHelper.Post", url: @params[0].ToString(), result: "", cost: (int)mil, method: (get ? "GET" : "POST"), mode: "Active", exp: ex, @params: list.ToArray())); } } finally { OnLogged?.Invoke(new APILog(level: 0, operationName: "调用日志", des: get ? "ApiHelper.Get" : "ApiHelper.Post", url: @params[0].ToString(), result: SerializeHelper.Serialize(result), cost: (int)mil, method: (get ? "GET" : "POST"), mode: "Active", exp: null, @params: list.ToArray())); stopwatch.Stop(); } return(result); }
/// <summary> /// 将实体发送给远程服务器 /// 发送json /// </summary> /// <param name="url"></param> /// <param name="obj"></param> /// <returns></returns> public static string Post(string url, Object obj) { return(Post(url, SerializeHelper.Serialize(obj))); }