Question Details

No question body available.

Tags

c# http .net-10.0

Answers (1)

Accepted Answer Available
Accepted Answer
May 7, 2026 Score: 5 Rep: 80,718 Quality: Expert Completeness: 80%

No, at least not without reflection or rebuilding HttpClient.

The headers are set, even when using TryAddWithoutValidation, using internal methods which eventually reach HeaderDescriptor and KnownHeaders which does an OrdinalIgnoreCase lookup.

The afore-mentioned reflection would be something like this, note that this is obviously subject to breaking changes without notice.

private static readonly object headerDescriptor = Activator.CreateInstance(
    typeof(HttpHeaders).Assembly.GetType("System.Net.Http.Headers.HeaderDescriptor"),
    BindingFlags.Instance | BindingFlags.NonPublic,
    null,
    ["Content-type", false],
    null);

private static readonly MethodInfo tryAddWithoutValidation = typeof(HttpHeaders) .GetMethod( "TryAddWithoutValidation", BindingFlags.Instance | BindingFlags.NonPublic, [ headerDescriptor.GetType(), typeof(string) ]) ?? throw new Exception("Can't find TryAddWithoutValidation method");

public static void AddContenttype(this HttpHeaders headers, string value) { tryAddWithoutValidation.Invoke(headers, [ _headerDescriptor, value ]); }

Do note that case-sensitive handling of HTTP headers is against the RFC 2616 specification and should be fixed:

4.2
...
Field names are case-insensitive.