程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> rest-用存儲的REST調用也是401錯誤~~

rest-用存儲的REST調用也是401錯誤~~

編輯:編程綜合問答
用存儲的REST調用也是401錯誤~~

我按照教程使用REST調用BLOB的內容,也遇到的是401錯誤,這是什麼情況?還有人也遇到了嗎?

最佳回答:


您好,
請問您使用的是REST中的哪一個方法?據我所知,如果使用REST去調用blob中內容是,復雜的地方主要是在構造簽名,首先我建議您先閱讀下這篇文檔:
https://msdn.microsoft.com/zh-cn/library/azure/dd179428.aspx
例如該方法:

 public void PutBlob(String containerName, String blobName)

{

    String requestMethod = "PUT";



    String urlPath = String.Format("{0}/{1}", containerName, blobName);



    String storageServiceVersion = "2012-02-12";



    String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);



    String content = "Andrew Carnegie was born in Dunfermline";

    UTF8Encoding utf8Encoding = new UTF8Encoding();

    Byte[] blobContent = utf8Encoding.GetBytes(content);

    Int32 blobLength = blobContent.Length;



    const String blobType = "BlockBlob";



    String canonicalizedHeaders = String.Format(

            "x-ms-blob-type:{0}\nx-ms-date:{1}\nx-ms-version:{2}",

            blobType,

            dateInRfc1123Format,

            storageServiceVersion);

    String canonicalizedResource = String.Format("/{0}/{1}", AzureStorageConstants.Account, urlPath);

    String stringToSign = String.Format(

            "{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}",

            requestMethod,

            blobLength,

            canonicalizedHeaders,

            canonicalizedResource);

    String authorizationHeader = Utility.CreateAuthorizationHeader(stringToSign);



    Uri uri = new Uri(AzureStorageConstants.BlobEndPoint + urlPath);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

    request.Method = requestMethod;

    request.Headers.Add("x-ms-blob-typeobType);

    request.Headers.Add("x-ms-dateteInRfc1123Format);

    request.Headers.Add("x-ms-versionorageServiceVersion);

    request.Headers.Add("AuthorizationthorizationHeader);

    request.ContentLength = blobLength;



    using (Stream requestStream = request.GetRequestStream())

    {

        requestStream.Write(blobContent, 0, blobLength);

    }



    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

    {

        String ETag = response.Headers["ETag"];

    }

}


我們需要構造出shared key,同時注意header中的各個信息。
處理rest問題和調試rest錯誤時,我個人建議您可以使用fiddler去troubleshoot您的reqest請求,去解決問題。
同時,建議您參考這個博客:https://convective.wordpress.com/2010/08/18/examples-of-the-windows-azure-storage-services-rest-api/
Regards,
Will
如果您想進一步了解Windows Azure, Windows Azure 官網歡迎您的訪問

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved