/// <summary> /// 获取大写的MD5签名结果 /// </summary> /// <param name="encypStr">需要加密的字符串</param> /// <param name="charset">编码</param> /// <returns></returns> public static string GetMD5(string encypStr, string charset) { string retStr; MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider(); //创建md5对象 byte[] inputBye; byte[] outputBye; //使用GB2312编码方式把字符串转化为字节数组. try { inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr); } catch (Exception ex) { AliTrace.Log("Exception on [Com.Alibaba.Helpers.EncryptHelper.GetMD5]", ex); inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr); } outputBye = m5.ComputeHash(inputBye); retStr = BitConverter.ToString(outputBye); retStr = retStr.Replace("-", "").ToUpper(); return(retStr); }
/// <summary> /// GET方式请求URL,并返回T类型 /// </summary> /// <typeparam name="T">接收JSON的数据类型</typeparam> /// <param name="url"></param> /// <param name="encoding"></param> /// <param name="maxJsonLength">允许最大JSON长度</param> /// <returns></returns> public static T GetJson <T>(string url, Encoding encoding = null, int?maxJsonLength = null) { string returnText = RequestUtility.HttpGet(url, encoding); AliTrace.SendApiLog(url, returnText); JavaScriptSerializer js = new JavaScriptSerializer(); if (maxJsonLength.HasValue) { js.MaxJsonLength = maxJsonLength.Value; } //if (returnText.Contains("errcode")) //当错误代码不为0时,发生错误 if (System.Text.RegularExpressions.Regex.IsMatch(returnText, @"(?isx)\x22error_?code\x22[\s\r\n]*\:[\s\r\n]*(\x22?)\w+\1")) { //可能发生错误 AliJsonResult errorResult = js.Deserialize <AliJsonResult>(returnText); if (errorResult.code != (int)ReturnCode.Success) { //发生错误 throw new ErrorJsonResultException( string.Format("请求发生错误!错误代码:{0},说明:{1}", (int)errorResult.code, errorResult.message), null, errorResult, url); } } T result = js.Deserialize <T>(returnText); return(result); }
/// <summary> /// ErrorJsonResultException /// </summary> /// <param name="message">异常消息</param> /// <param name="inner">内部异常</param> /// <param name="jsonResult">WxJsonResult</param> /// <param name="url">API地址</param> public ErrorJsonResultException(string message, Exception inner, AliJsonResult jsonResult, string url = null) : base(message, inner, true) { JsonResult = jsonResult; Url = url; AliTrace.ErrorJsonResultExceptionLog(this); }
/// <summary> /// 【异步方法】PostGetJson的异步版本 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="cookieContainer"></param> /// <param name="fileStream"></param> /// <param name="encoding"></param> /// <param name="cer">证书,如果不需要则保留null</param> /// <param name="timeOut"></param> /// <param name="checkValidationResult"></param> /// <returns></returns> public static async Task <T> PostGetJsonAsync <T>(string url , CookieContainer cookieContainer = null , Stream fileStream = null , Encoding encoding = null , X509Certificate cer = null , int timeOut = Config.TIME_OUT , bool checkValidationResult = false) { string returnText = await RequestUtility.HttpPostAsync(url, cookieContainer, fileStream, null, null, encoding, cer, timeOut, checkValidationResult); AliTrace.SendApiLog(url, returnText); var result = GetResult <T>(returnText); return(result); }
/// <summary> /// 反序列化 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="xml">XML字符串</param> /// <returns></returns> public static object Deserialize <T>(string xml) { try { using (StringReader sr = new StringReader(xml)) { XmlSerializer xmldes = new XmlSerializer(typeof(T)); return(xmldes.Deserialize(sr)); } } catch (Exception e) { AliTrace.Log("Deserialize Exception [Com.Alibaba.Utilities.XmlUtility.Deserialize]", e); return(null); } }
/// <summary> /// /// </summary> /// <param name="resourceName"></param> /// <param name="retryCount"></param> /// <param name="retryDelay"></param> /// <returns></returns> public override bool Lock(string resourceName, int retryCount, TimeSpan retryDelay) { int currentRetry = 0; int maxRetryDelay = (int)retryDelay.TotalMilliseconds; while (currentRetry++ < retryCount) { #region 尝试并获得锁 var getLock = false; try { lock (lookPoolLock) { if (LockPool.ContainsKey(resourceName)) { getLock = false;//已被别人锁住,没有取得锁 } else { LockPool.Add(resourceName, new object()); //创建锁 getLock = true; //取得锁 } } } catch (Exception ex) { AliTrace.Log("本地同步锁发生异常:" + ex.Message);; getLock = false; } #endregion if (getLock) { return(true);//取得锁 } Thread.Sleep(_rnd.Next(maxRetryDelay)); } return(false); }