0

I am attempting to use HttpClient to upload a file to Microsoft Azure Blob Storage via their REST api in Xamarin.iOS. It's been going alright until now. Every time I try to add Content-Length header to the client I get this error:

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 

This is my code for creating the 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);

I tried using TryAddWithoutValidation instead of Add:

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

The error doesn't get thrown but the header still doesn't get added. Any help would be great.

JacobK
  • 53
  • 8

1 Answers1

0

Here's the inner workings of CheckName(), which is throwing the exception. You can find the source here: 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;
    }

After looking at the full source file:

It looks like Content-Length is in the collection of known_headers.

Also looks like the internal type of the Content-Length header value is a long. But the Add() method only take a string for the value, which get's parsed to a long. Is the string value that you're passing for the Content-Length value a valid long?

NovaJoe
  • 4,595
  • 6
  • 29
  • 43