diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderDescriptor.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderDescriptor.cs index 6bb9a80a62e8a..fac5d58bf282d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderDescriptor.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderDescriptor.cs @@ -161,7 +161,8 @@ public string GetHeaderValue(ReadOnlySpan headerValue, Encoding? valueEnco else if (knownHeader == KnownHeaders.Location) { // Normally Location should be in ISO-8859-1 but occasionally some servers respond with UTF-8. - if (TryDecodeUtf8(headerValue, out string? decoded)) + // If the user set the ResponseHeaderEncodingSelector, we give that priority instead. + if (valueEncoding is null && TryDecodeUtf8(headerValue, out string? decoded)) { return decoded; } diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs index 1cde26484b42f..663e3e3a4e148 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs @@ -441,7 +441,8 @@ private static readonly (string Name, Encoding ValueEncoding, string Separator, ("Cookie", Encoding.UTF8, "; ", new[] { "Cookies", "\uD83C\uDF6A", "everywhere" }), ("Set-Cookie", Encoding.UTF8, ", ", new[] { "\uD83C\uDDF8\uD83C\uDDEE" }), ("header-5", Encoding.UTF8, ", ", new[] { "\uD83D\uDE48\uD83D\uDE49\uD83D\uDE4A", "foo", "\uD83D\uDE03", "bar" }), - ("bar", Encoding.UTF8, ", ", new[] { "foo" }) + ("bar", Encoding.UTF8, ", ", new[] { "foo" }), + ("Location", Encoding.Latin1, ", ", new[] { "\u00D0\u00A4" }) }; [Fact] @@ -538,7 +539,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync( foreach ((string name, Encoding valueEncoding, string separator, string[] values) in s_nonAsciiHeaders) { Assert.Contains(name, seenHeaderNames); - IEnumerable receivedValues = Assert.Single(response.Headers, h => h.Key.Equals(name, StringComparison.OrdinalIgnoreCase)).Value; + IEnumerable receivedValues = Assert.Single(response.Headers.NonValidated, h => h.Key.Equals(name, StringComparison.OrdinalIgnoreCase)).Value; string value = Assert.Single(receivedValues); string expected = valueEncoding.GetString(valueEncoding.GetBytes(string.Join(separator, values)));