/// <summary> /// 对数据进行签名 /// </summary> /// <param name="requestMethod">请求方法.如GET或POST</param> /// <param name="requestUrl">API请求地址</param> /// <param name="parameters">提交参数</param> /// <returns></returns> public string GenerateSignature(string requestMethod, string requestUrl, Parameters parameters) { HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", Util.UrlEncode(this.AppSecret), Util.UrlEncode(this.TokenSecret))); StringBuilder data = new StringBuilder(100); data.AppendFormat("{0}&{1}&", requestMethod.ToUpper(), Util.UrlEncode(requestUrl)); //处理参数 if (parameters != null) { parameters.Sort(); data.Append(Util.UrlEncode(parameters.BuildQueryString(true))); } byte[] dataBuffer = System.Text.Encoding.ASCII.GetBytes(data.ToString()); byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer); return Convert.ToBase64String(hashBytes); }