30

Is Request.Headers["Header-Name"] in ASP.NET case-sensitive? And if it is, how should I get a certain header (e.g. "X-requested-with") if I don't know for sure what case the client will send it in?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
SlimShaggy
  • 2,917
  • 4
  • 22
  • 23

3 Answers3

27

no they are case-insensitive as per RFC2616

4.2 Message Headers

HTTP header fields, which include general-header (section 4.5),
request-header (section 5.3), response-header (section 6.2), and
entity-header (section 7.1) fields, follow the same generic format as that given in Section 3.1 of RFC 822 [9]. Each header field consists
of a name followed by a colon (":") and the field value. Field names
are case-insensitive. The field value MAY be preceded by any amount
of LWS, though a single SP is preferred. Header fields can be
extended over multiple lines by preceding each extra line with at
least one SP or HT. Applications ought to follow "common form", where one is known or indicated, when generating HTTP constructs, since
there might exist some implementations that fail to accept anything

HatSoft
  • 11,077
  • 3
  • 28
  • 43
6

Request.Headers is case-insensitive.

Borrowing from this answer:

From RFC 2616, "Hypertext Transfer Protocol -- HTTP/1.1", ยง4.2, "Message Headers":

Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive.

Community
  • 1
  • 1
boflynn
  • 3,534
  • 1
  • 27
  • 28
  • 11
    From the remarks on [`NameValueCollection`](http://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx) (which is the implementation type of [`HttpRequest.Headers`](http://msdn.microsoft.com/en-us/library/system.web.httprequest.headers.aspx)): `The hash code provider dispenses hash codes for keys in the NameValueCollection. The default hash code provider is the CaseInsensitiveHashCodeProvider.` โ€“ mellamokb Jul 23 '12 at 17:02
  • 2
    "Headers" is a NameValueCollection and the key is not case sensitive. "QueryString" and "Form" are also NVC's and as such are not case sensitive either unless the comparer is changed โ€“ Nick Bork Jul 23 '12 at 17:03
0

I never used ASP.NET but RFC HTTP/1.1 defines that message-headers field-name are case insensitive.

If ASP.NET follow HTTP Specification, Request.Header["Header-Name"] will return the same value that Request.Header["header-name"].

Miliox
  • 11
  • 3