我只为 API REST 编写了 4 种方法,但我需要的最后一种方法不起作用。这是删除消息队列。我有这个代码:
public static string DeleteMessage(String queueName, string account, byte[] key, string endpoint, string popreceipt,string messageid)
{
string requestMethod = "DELETE";
String urlPath = String.Format("{0}/messages/{1}?popreceipt={2}", queueName,Uri.EscapeDataString(messageid),Uri.EscapeDataString(popreceipt));
String storageServiceVersion = "2009-09-19";
String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
String canonicalizedHeaders = String.Format(
"x-ms-date:{0}\nx-ms-version:{1}",
dateInRfc1123Format,
storageServiceVersion);
String canonicalizedResource = String.Format("/{0}/{1}", account, urlPath);
//String canonicalizedResource = String.Format("/{0}/{1}\npopreceipt:{2}", account, urlPath, popreceipt);
String stringToSign = String.Format(
"{0}\n\n\n\n\n\n\n\n\n\n\n\n{1}\n{2}",
requestMethod,
canonicalizedHeaders,
canonicalizedResource);
String authorizationHeader = CreateAuthorizationHeader(stringToSign, account, key);
Uri uri = new Uri(endpoint + urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = requestMethod;
request.Headers.Add("x-ms-date", dateInRfc1123Format);
request.Headers.Add("x-ms-version", storageServiceVersion);
request.Headers.Add("Authorization", authorizationHeader);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream dataStream = response.GetResponseStream();
return response.StatusCode.ToString();
}
}
public static string GetMessage(String queueName,string account, byte[] key,string endpoint)
{
string requestMethod = "GET";
String urlPath = String.Format("{0}/messages", queueName);
String storageServiceVersion = "2009-09-19";
String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
String canonicalizedHeaders = String.Format(
"x-ms-date:{0}\nx-ms-version:{1}",
dateInRfc1123Format,
storageServiceVersion );
String canonicalizedResource = String.Format("/{0}/{1}", account, urlPath);
String stringToSign = String.Format(
"{0}\n\n\n\n\n\n\n\n\n\n\n\n{1}\n{2}",
requestMethod,
canonicalizedHeaders,
canonicalizedResource);
String authorizationHeader = CreateAuthorizationHeader(stringToSign,account,key);
Uri uri = new Uri(endpoint + urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = requestMethod;
request.Headers.Add("x-ms-date", dateInRfc1123Format);
request.Headers.Add("x-ms-version", storageServiceVersion );
request.Headers.Add("Authorization", authorizationHeader);
request.Accept = "application/atom+xml,application/xml";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream dataStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(dataStream))
{
String responseFromServer = reader.ReadToEnd();
return responseFromServer;
}
}
}
GetMessage 正常工作,而 DeleteMessage 不工作,CreateAuthorithation 标头代码为:
private static String CreateAuthorizationHeader(String canonicalizedString, string account, byte[] key)
{
String signature = string.Empty;
using (HMACSHA256 hmacSha256 = new HMACSHA256(key))
{
Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(canonicalizedString);
signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}
String authorizationHeader = String.Format(
CultureInfo.InvariantCulture,
"{0} {1}:{2}",
"SharedKey",
account,
signature);
return authorizationHeader;
}
我使用“Convert.FromBase64String(AccountSharedKey)”传递密钥,该密钥适用于 GetMessage(以及另一种放置消息的方法),但这不适用于删除消息。
我看到 MSDN 中的 API 和 DELETE 和 GET 消息使用相同的参数,除了查询字符串传递的参数。
解决了
问题是我不使用 UriEscape 作为 Uri 字符串中的参数,有时 URL 有效,有时无效。