Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin iOS将内容长度头添加到HttpClient引发错误_Ios_Xamarin_Httpclient_Azure Storage Blobs_Content Length - Fatal编程技术网

Xamarin iOS将内容长度头添加到HttpClient引发错误

Xamarin iOS将内容长度头添加到HttpClient引发错误,ios,xamarin,httpclient,azure-storage-blobs,content-length,Ios,Xamarin,Httpclient,Azure Storage Blobs,Content Length,我正在尝试使用HttpClient通过Xamarin.iOS中的REST api将文件上载到Microsoft Azure Blob存储。直到现在一切都很顺利。每次尝试向客户端添加内容长度标题时,都会出现以下错误: System.InvalidOperationException: Content-Length\n at System.Net.Http.Headers.HttpHeaders.CheckName (System.String name) [0x0005f] in /Develo

我正在尝试使用HttpClient通过Xamarin.iOS中的REST api将文件上载到Microsoft Azure Blob存储。直到现在一切都很顺利。每次尝试向客户端添加内容长度标题时,都会出现以下错误:

System.InvalidOperationException: Content-Length\n  at System.Net.Http.Headers.HttpHeaders.CheckName (System.String name) [0x0005f] in /Developer/MonoTouch/Source/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs:253 \n  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, IEnumerable`1 values) [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs:171 \n  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, System.String value) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs:163 
这是我创建HttpClient的代码

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Content-Length", blobLength.ToString()); // Error here
client.DefaultRequestHeaders.Add("x-ms-date", dateInRfc1123Format);
client.DefaultRequestHeaders.Add("x-ms-version", msVersion);
Debug.WriteLine("Added all headers except Authorization");
client.DefaultRequestHeaders.Add("Authorization", authorizationHeader);


Debug.WriteLine("Added Authorization header");
//logRequest(requestContent, uri);

Debug.WriteLine("created new http client");
HttpContent requestContent = new ByteArrayContent(blobckContent);
HttpResponseMessage response = await client.PutAsync(uri, requestContent);
我尝试在没有验证的情况下使用tryadd,而不是添加:

client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Length", blobLength.ToString());
不会抛出错误,但仍不会添加标头。
任何帮助都很好。

以下是CheckName的内部工作原理,它将引发异常。您可以在此处找到来源:

查看完整的源文件后:

内容长度似乎在已知的_头的集合中

此外,内容长度标题值的内部类型看起来也是long。但是Add方法只接受一个字符串作为值,get被解析为long。为内容长度值传递的字符串值是否有效

HeaderInfo CheckName (string name)
    {
        if (string.IsNullOrEmpty (name))
            throw new ArgumentException ("name");

        Parser.Token.Check (name);

        HeaderInfo headerInfo;
        if (known_headers.TryGetValue (name, out headerInfo) && (headerInfo.HeaderKind & HeaderKind) == 0) {
            if (HeaderKind != HttpHeaderKind.None && ((HeaderKind | headerInfo.HeaderKind) & HttpHeaderKind.Content) != 0)
                throw new InvalidOperationException (name);

            return null;
        }

        return headerInfo;
    }