neverendingqs
neverendingqs

Reputation: 4276

Does the "name" parameter for "HttpHeaders.TryGetValues()" care about case sensitivity?

According to https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2,

Field names are case-insensitive.

Does the method HttpHeaders.TryGetValues() comply with the protocol? (i.e. does the method knows to not take case-sensitivity of the name parameter into account?

Upvotes: 5

Views: 3294

Answers (2)

Luaan
Luaan

Reputation: 63732

As noted on the MSDN page,

A collection of headers and their values as defined in RFC 2616.

So officially, it's part of the contract.

How's reality?

As per the source code, the dictionaries used to store the headers are case insensitive:

new Dictionary<string, HeaderStoreItemInfo>(StringComparer.OrdinalIgnoreCase)

Upvotes: 14

neverendingqs
neverendingqs

Reputation: 4276

See @Luaan's answer as well.

Sample code:

HttpRequestMessage hrh = new HttpRequestMessage();
HttpHeaders headers = hrh.Headers;
headers.Add( "ALLCAPS", "thevalue" );

IEnumerable<string> headerValues;
bool success = headers.TryGetValues( "allcaps", out headerValues );
Assert.IsTrue( success );

Console.Out.WriteLine( String.Join( ",", headerValues ) );
// thevalue

Upvotes: 1

Related Questions