ModernHttpClient在Xamarin Android

时间:2015-06-29 20:32:28

标签: xamarin.android azure-storage-blobs dotnet-httpclient

我正在尝试更新现有的,有效的Xamarin应用程序以使用ModernHttpClient。该应用程序使用Azure REST调用来创建容器并将Blob上载到Azure存储。

当我为每个new NativeMessageHandler()语句添加new HttpClient()以启用ModernHttpClient时,Azure容器继续创建没有任何问题,但上传Blob的调用返回Forbidden。

这是上传blob的代码(源自其他人的工作):

private async Task<bool> PutBlob(String containerName, String blobName, string photoPath)       
 {
            byte[] blobContent = System.IO.File.ReadAllBytes(photoPath);            

            const String blobType = "BlockBlob";
            String requestMethod = "PUT";
            Int32 blobLength = blobContent.Length;

            String urlPath = String.Format("{0}/{1}", containerName, blobName);
            String msVersion = "2009-09-19";
            String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);

            String canonicalizedHeaders = String.Format("x-ms-blob-type:{0}\nx-ms-date:{1}\nx-ms-version:{2}", blobType, dateInRfc1123Format, msVersion);
            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 = SignThis(stringToSign);

            string uri = AzureStorageConstants.BlobEndPoint + urlPath;
            HttpClient client = new HttpClient(new NativeMessageHandler());
            client.DefaultRequestHeaders.Add("x-ms-blob-type", blobType);
            client.DefaultRequestHeaders.Add("x-ms-date", dateInRfc1123Format);
            client.DefaultRequestHeaders.Add("x-ms-version", msVersion);
            client.DefaultRequestHeaders.Add("Authorization", authorizationHeader);

            bool isSuccess = false;
            var UploadBlobTimer = Insights.TrackTime("UploadBlobTime");
            UploadBlobTimer.Start();
                using (HttpContent requestContent = new ByteArrayContent(blobContent))
                {
                    HttpResponseMessage response = await client.PutAsync(uri, requestContent);
                    isSuccess = response.IsSuccessStatusCode;

                }
            UploadBlobTimer.Stop();           

            return isSuccess;            
        }

更新

以下是成功的.Net和不成功的Modern PutBlob电话的响应标题:

.Net

Transfer-Encoding: chunked  
ETag: 0x8D280EE1D2ACEFF  
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0  
x-ms-request-id: 0a35e186-0001-0056-4dd6-b22ebd000000  
x-ms-version: 2009-09-19  
Date: Tue, 30 Jun 2015 01:49:18 GMT  

现代

Date: Tue, 30 Jun 2015 01:51:22 GMT  
OkHttp-Received-Millis: 1435629095708  
OkHttp-Selected-Protocol: http/1.1  
OkHttp-Sent-Millis: 1435629083933  
Server: Microsoft-HTTPAPI/2.0  
x-ms-request-id: c18abaec-0001-0033-09d7-b29fe0000000  

感谢任何帮助!

0 个答案:

没有答案