后台方法: |
string AccessKeyId = ""; //密钥ID string AccessKeySecret = ""; string Format = "JSON"; //返回值的类型 string Version = "2015-01-09"; //API版本号 string Signature = ""; //签名结果串 string SignatureMethod = "HMAC-SHA1"; //签名方式,目前支持HMAC-SHA1 string Timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ssZ"); //请求的时间戳 日期格式按照ISO8601标准表示,并需要使用UTC时间 YYYY-MM-DDThh:mm:ssZ; string SignatureVersion = "1.0"; //签名算法版本 string SignatureNonce = Guid.NewGuid().ToString(); //唯一随机数,用于防止网络重放攻击。用户在不同请求间要使用不同的随机数值 string InterFaceUrl = "http://alidns.aliyuncs.com/"; //请求路径 /// <summary> /// 计算签名 /// </summary> public void ComputeSignature( Dictionary<string, string> ditParam,string method = "GET") { BuildParameters(ditParam); //按ascii码排序 Dictionary<string, string> asciiDit = new Dictionary<string, string>(); string[] KeyArr = ditParam.Keys.ToArray(); Array.Sort(KeyArr, string.CompareOrdinal); foreach (var key in KeyArr) { string value = ditParam[key]; asciiDit.Add(key, value); } //计算签名 var canonicalizedQueryString = string.Join("&", asciiDit.Select(x => PercentEncode(x.Key) + "=" + PercentEncode(x.Value))); var stringToSign = method.ToString().ToUpper() + "&" + PercentEncode("/") + "&" + PercentEncode(canonicalizedQueryString); var keyBytes = Encoding.UTF8.GetBytes(AccessKeySecret + "&"); var hmac = new HMACSHA1(keyBytes); var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)); Signature = Convert.ToBase64String(hashBytes); ditParam.Add("Signature", Signature); }
} private static string PercentEncode(string value) { return UpperCaseUrlEncode(value) .Replace("/", "%2F") .Replace("+", "%20") .Replace("*", "%2A") .Replace("%7E", "~") .Replace("!", "%21") .Replace("'", "%27") .Replace("(", "%28") .Replace(")", "%29"); } private static string UpperCaseUrlEncode(string s) { // C# 的 HttpUtility.UrlEncode() 编码得到的形如 %xx%xx的内容是小写的,Java 的是大写的。 char[] temp = HttpUtility.UrlEncode(s).ToCharArray(); for (int i = 0; i < temp.Length - 2; i++){ if (temp[i] == '%') { temp[i + 1] = char.ToUpper(temp[i + 1]); temp[i + 2] = char.ToUpper(temp[i + 2]); } } return new string(temp); } public static string HttpGetUrl(string Url, string postDataStr) |
方法调用: |
/// <summary> /// 修改域名解析记录值 /// </summary> /// <param name="RRKeyWordValue">主机记录的关键字</param> /// <returns></returns> public void UpdateByRecordId(string RecordId) { Dictionary<string, string> ditParam = new Dictionary<string, string>(); ditParam.Add("Action", "UpdateDomainRecord"); ditParam.Add("RecordId", RecordId); ditParam.Add("RR", "www");//主机记录的关键字 ditParam.Add("Type", "A");//解析类型的关键字 ditParam.Add("Value", "192.168.0.130");//记录值的关键字 ComputeSignature(ditParam); string param = string.Join("&", ditParam.Select(x => x.Key + "=" + HttpUtility.UrlEncode(x.Value))); string url = InterFaceUrl + "?" + param; string result = HttpHelper.HttpGetUrl(InterFaceUrl, param); HttpContext.Current.Response.Write(result); HttpContext.Current.Response.End(); } |