我调用云解析老是报
SignatureDoesNotMatch错误,求解决,程序语言是C#,代码如下:
public class AliyunDNSUtil
{
public static void UpdateDNS(string ip)
{
var x = GetDomainList();
}
private static string UrlEncoder(string value)
{
return HttpUtility.UrlEncode(value).Replace("+", "%20").Replace("*", "%2A").Replace("%7E", "~");
}
private static SortedDictionary<string, string> _PublicArguments;
private static SortedDictionary<string, string> PublicArguments
{
get
{
if (_PublicArguments == null)
{
var result = new SortedDictionary<string, string>();
result.Add("Format", UrlEncoder("XML"));
result.Add("Version", UrlEncoder("2015-01-09"));
result.Add("AccessKeyId", UrlEncoder(Settings.AliyunAccessKeyID));
result.Add("SignatureMethod", UrlEncoder("HMAC-SHA1"));
result.Add("Timestamp", UrlEncoder(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")));
result.Add("SignatureVersion", UrlEncoder("1.0"));
result.Add("SignatureNonce", UrlEncoder(Guid.NewGuid().ToString("D")));
_PublicArguments = result;
}
return _PublicArguments;
}
}
private static string StringToSign(string value)
{
Encoding encode = Encoding.UTF8;
byte[] byteData = encode.GetBytes(value);
byte[] byteKey = encode.GetBytes(Settings.AliyunAccessKeySecret + "&");
HMACSHA1 hmac = new HMACSHA1(byteKey);
CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write);
cs.Write(byteData, 0, byteData.Length);
cs.Close();
return Convert.ToBase64String(hmac.Hash);
}
private static XDocument GetDomainList()
{
var arguments = new SortedDictionary<string, string>();
foreach (var item in PublicArguments.Keys)
{
arguments.Add(item, PublicArguments[item]);
}
arguments.Add("Action", UrlEncoder("DescribeDomains"));
arguments.Add("KeyWord", UrlEncoder(Settings.DomainName));
var argumentssb = new StringBuilder();
foreach (var item in arguments.Keys)
{
argumentssb.Append("&" + item + "=" + arguments[item]);
}
var argumentsstr = argumentssb.ToString().Substring(1);
var signstring = "GET&%2F&" + UrlEncoder(argumentsstr);
var encodedargs = argumentsstr + "&Signature=" + UrlEncoder(StringToSign(signstring));
var url = "https://dns.aliyuncs.com/?" + encodedargs;
var xmlstream = HttpRequest(url);
if (xmlstream == null)
{
return null;
}
var reader = new StreamReader(xmlstream, Encoding.UTF8);
var str = reader.ReadToEnd();
return XDocument.Load(xmlstream);
}
private static Stream HttpRequest(string url)
{
try
{
var httpReq = (HttpWebRequest)WebRequest.Create(url);
var httpResp = (HttpWebResponse)httpReq.GetResponse();
return httpResp.GetResponseStream();
}
catch (Exception)
{
return null;
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。