Sean Feldman Sean Feldman - 1 year ago 173
C# Question

HttpResponseMessage.Content.Headers ContentDisposition is null

When downloading a file with HttpClient, I'm downloading first the headers and then the content. When headers are downloaded, I can see Headers collection on the Content property of HttpResponseMessage, but when accessing it through ContentDisposition on Headers, get null

Why this is happening? Fiddler shows headers are fine...


var responseMessage = await httpClient.GetAsync(uri,
HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(continueOnCapturedContext: false);

Update 1

It looks like this class is following Content-Disposition implementation outlined in RFC 2616 and fails to handle Content-Disposition implementation update RFC 6266. RFC 2616 defines
parameter value to be a quoted-string, where update RFC 6266 just states it should be value.

RFC 2616 Grammar

content-disposition = "Content-Disposition" ":"
disposition-type *( ";" disposition-parm )
disposition-type = "attachment" | disp-extension-token
disposition-parm = filename-parm | disp-extension-parm
filename-parm = "filename" "=" quoted-string
disp-extension-token = token
disp-extension-parm = token "=" ( token | quoted-string )

RFC 6266 Grammar

content-disposition = "Content-Disposition" ":"
disposition-type *( ";" disposition-parm )

disposition-type = "inline" | "attachment" | disp-ext-type
; case-insensitive
disp-ext-type = token

disposition-parm = filename-parm | disp-ext-parm

filename-parm = "filename" "=" value
| "filename*" "=" ext-value

disp-ext-parm = token "=" value
| ext-token "=" ext-value
ext-token = <the characters in token, followed by "*">

ext-value = <ext-value, defined in [RFC5987], Section 3.2>


Working case

Working case

Failing case

Failing case

Update 2

Opened a ticket with MS connect.

Update 3

Microsoft has acknowledged that this is a bug and will fix it.

Answer Source

The problem is with the trailing ; in the content-disposition header

        public void ParseContentDispositionHeader()
            var value = ContentDispositionHeaderValue.Parse("attachment; filename=GeoIP2-City_20140107.tar.gz");

If I add the semi-colon the parsing will fail. If you look at the RFC6266 grammar, the semi-colon is only supposed to precede the parameter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download