0
votes

I'm making the following request to a local website running in IIS

var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.RequestUri = new Uri("http://localhost:8081/");
httpRequestMessage.Method = HttpMethod.Get;

var response = new HttpClient().SendAsync(httpRequestMessage).Result;

This produces the following response headers:

HTTP/1.1 200 OK
Accept-Ranges: bytes
Date: Mon, 03 Jun 2013 22:34:25 GMT
ETag: "50c7472eb342ce1:0"
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET

An identical request made via Fiddler produces the following response headers (I've highlighted the differences):

HTTP/1.1 200 OK
Content-Type: text/html
Last-Modified: Fri, 26 Apr 2013 19:20:58 GMT
Accept-Ranges: bytes
ETag: "50c7472eb342ce1:0"
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Date: Mon, 03 Jun 2013 22:29:34 GMT
Content-Length: 10

Why is there a difference in response headers?
Am I using HttpClient correctly (aside from the fact I am calling Send synchronously)?

1
Have you verified the requests are actually identical? Could you post both requests here too?svick

1 Answers

1
votes

TL;DR;

To access all response headers you need to read both HttpResponseMessage.Headers and HttpResponseMessage.Content.Headers properties.

Long(er) answer:

This, basically:

var response = new HttpClient().GetAsync("http://uri/").Result;
var allHeaders = response.Headers.Union(response.Content.Headers);

foreach (var header in allHeaders)
{
    // do stuff
}

I see two issues with this:

  • The Headers property is not appropriately named: it should really be SomeHeaders or AllHeadersExceptContentHeaders. (I mean, really, when you see a property named Headers, do you expect it to return all headers or some headers? I am pretty sure they are in violation of their own framework design guidelines on this one.)
  • The MSDN page does not mention at any point the fact this is a subset of all headers and developers should also inspect Content.Headers.