Closed
Description
Repro code:
request.Headers.Add("Accept", "text/foo");
request.Headers.TryAddWithoutValidation("Accept", "text/bar");
Console.WriteLine("NonValidated before parsing:");
PrintHeadersNonValidated(request);
// Force parsing
_ = request.Headers.Accept.Count;
Console.WriteLine("NonValidated after parsing:");
PrintHeadersNonValidated(request);
static void PrintHeadersNonValidated(HttpRequestMessage request)
{
foreach (var header in request.Headers.NonValidated)
{
Console.WriteLine($" {header.Key}:");
foreach (var value in header.Value)
{
Console.WriteLine($" {value}");
}
}
Console.WriteLine();
}
Output:
NonValidated before parsing (incorrect):
Accept:
text/bar
text/foo
NonValidated after parsing (correct):
Accept:
text/foo
text/bar
Note that this behavior is not limited to NonValidated -- it affects how the request headers are written to the wire when the request is sent.
Mixing typed Add with untyped TryAddWithoutValidation is not a common thing to do, so the impact here is small.