Xamarin.forms 如何使用System.Net.Http.HttpClient for Azure表存储正确设置content.Headers.ContentMD5属性

Xamarin.forms 如何使用System.Net.Http.HttpClient for Azure表存储正确设置content.Headers.ContentMD5属性,xamarin.forms,httpclient,azure-storage,md5,azure-table-storage,Xamarin.forms,Httpclient,Azure Storage,Md5,Azure Table Storage,我成功地将Azure表存储的RESTAPI与HttpWebRequest一起使用 现在,我正在尝试将应用程序移植到System.Net.Http.HttpClient类。 为了使用sharedKey方法进行身份验证,将Content-MD5头设置为 content.Headers.Add("Content-MD5", hashString); 这也适用于UWP和HttpClient类,但不适用于iOS(在Fiddler捕获的请求中,Content-MD5头的值为空) 在HttpClient中,

我成功地将Azure表存储的RESTAPI与HttpWebRequest一起使用

现在,我正在尝试将应用程序移植到
System.Net.Http.HttpClient类。

为了使用sharedKey方法进行身份验证,将
Content-MD5
头设置为

content.Headers.Add("Content-MD5", hashString);
这也适用于UWP和HttpClient类,但不适用于iOS(在Fiddler捕获的请求中,Content-MD5头的值为空)

在HttpClient中,现在应该使用
content.Headers.ContentMD5
属性

但是,我无法以Fiddler在UWP解决方案中为Content-MD5头显示相同值的方式设置此属性。 这是我的密码:

string contentString = "<some xml content>";

       // alternative hash function working on all platforms
       // byte[] hash = xBrainLab.Security.Cryptography.MD5.GetHash  (contentString);
       // string hashString = xBrainLab.Security.Cryptography.MD5.GetHashString(contentString);

        System.Security.Cryptography.MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();

        var hash = csp.ComputeHash(Encoding.UTF8.GetBytes(contentString));

        var hashString = ByteArrayToString(hash);  // is "AABB88AFD4056C0B8E4FEB6B433D5EE9"

        System.Net.Http.HttpClient client = new HttpClient();
        Uri uri = new Uri("http://woschmi01.table.core.windows.net/Test2018()");
        HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod("PUT"), uri);
        var content = new StringContent(contentString);

        // former solution, works on UWP but not on iOS
        content.Headers.Add("Content-MD5", hashString);

        // solution I'm trying to get working:

        // content.Headers.ContentMD5 = hash;                // What has to be taken as content.Headers.ContentMD5 property   ?????


        var response = SendRequest(client, uri, content);


        for ( int i = 0; i < 5; i++)
        {
            Thread.Sleep(1000);
        }

//****************************************
async Task<HttpResponseMessage> SendRequest(HttpClient client, Uri uri,    StringContent content)
        {
            HttpResponseMessage response =  await client.PostAsync(uri, content);
            return response;}

//**************************************       
 static string ByteArrayToString(byte[] ba)
    {
        return BitConverter.ToString(ba).Replace("-", "");
    }
string contentString=”“;
//在所有平台上工作的可选哈希函数
//byte[]hash=xBrainLab.Security.Cryptography.MD5.GetHash(contentString);
//string hashString=xBrainLab.Security.Cryptography.MD5.GetHashString(contentString);
System.Security.Cryptography.MD5CryptoServiceProvider csp=新的MD5CryptoServiceProvider();
var hash=csp.ComputeHash(Encoding.UTF8.GetBytes(contentString));
var hashString=ByteArrayToString(hash);//是“AABB88AFD4056C0B8E4FEB6B433D5EE9”
System.Net.Http.HttpClient client=新的HttpClient();
Uri=新的Uri(“http://woschmi01.table.core.windows.net/Test2018()");
HttpRequestMessage requestMessage=新的HttpRequestMessage(新的HttpMethod(“PUT”),uri);
var content=新的StringContent(contentString);
//前一种解决方案,适用于UWP,但不适用于iOS
content.Headers.Add(“content-MD5”,hashString);
//解决方案我正在努力工作:
//content.Headers.ContentMD5=hash;//什么必须作为content.Headers.ContentMD5属性?????
var response=SendRequest(客户端、uri、内容);
对于(int i=0;i<5;i++)
{
睡眠(1000);
}
//****************************************
异步任务SendRequest(HttpClient客户端、Uri、StringContent)
{
HttpResponseMessage response=wait client.PostAsync(uri,内容);
返回响应;}
//**************************************       
静态字符串ByteArrayToString(字节[]ba)
{
返回BitConverter.ToString(ba).替换(“-”,”);
}

我花了好几个小时寻找解决方案,并详细阐述了这个问题,最后我自己找到了答案:

 content.Headers.ContentMD5 =  Convert.FromBase64String(hashString);