【问题标题】:Xamarin iOS add Content-Length header to HttpClient throws errorXamarin iOS 向 HttpClient 添加 Content-Length 标头会引发错误
【发布时间】:2015-04-07 03:07:50
【问题描述】:

我正在尝试使用 HttpClient 通过他们在 Xamarin.iOS 中的 REST api 将文件上传到 Microsoft Azure Blob 存储。一直到现在都还好。每次我尝试向客户端添加 Content-Length 标头时,都会收到此错误:

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);

我尝试使用 TryAddWithoutValidation 代替 Add:

client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Length", blobLength.ToString());

错误没有被抛出,但标题仍然没有被添加。 任何帮助都会很棒。

【问题讨论】:

    标签: ios xamarin httpclient azure-blob-storage content-length


    【解决方案1】:

    这是 CheckName() 的内部工作原理,它引发了异常。你可以在这里找到来源:https://github.com/mono/mono/blob/master/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs

    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;
        }
    

    查看完整源文件后:

    看起来Content-Lengthknown_headers的集合中。

    看起来Content-Length 标头值的内部类型也很长。但是 Add() 方法只接受一个字符串作为值,它被解析为一个 long。您为 Content-Length 值传递的字符串值是有效的长字符串吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 2020-07-15
      • 2022-06-10
      • 1970-01-01
      • 2017-12-30
      相关资源
      最近更新 更多