Skip to content

Instantly share code, notes, and snippets.

@dustinsoftware
Last active July 18, 2018 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinsoftware/424ba34074f4044a1420713bda37104a to your computer and use it in GitHub Desktop.
Save dustinsoftware/424ba34074f4044a1420713bda37104a to your computer and use it in GitHub Desktop.
HttpClient headers repro
using System;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace HttpClientTest
{
class Program
{
public static void Main(string[] args)
{
DoWorkAsync().GetAwaiter().GetResult();
}
public static async Task DoWorkAsync()
{
using (var httpClientHandler = new HttpClientHandler())
{
using (var client = new HttpClient(httpClientHandler))
{
Console.WriteLine(RuntimeInformation.FrameworkDescription);
Console.WriteLine(Assembly.GetAssembly(typeof(HttpClient)));
var response = (await client.GetAsync("http://localhost:5000/api/values"));
response.Headers.TryGetValues(_serverKey, out var serverName);
// This always works
Console.WriteLine($"Headers.TryGetValues({_serverKey}): " + serverName?.First());
// Using `TryGetValues("Authorization")` returns empty string with .NET Core 2.1, but not with .NET Framework 4.7
response.Headers.TryGetValues(_authorizationKey, out var authorization);
Console.WriteLine($"Headers.TryGetValues({_authorizationKey}): {authorization?.First()}");
// Iterating over Headers works as expected, though
Console.WriteLine($"Headers.First(x => x.Key == {_authorizationKey}): {response.Headers.First(x => x.Key == _authorizationKey).Value.FirstOrDefault()}");
}
}
}
const string _serverKey = "Server";
const string _authorizationKey = "Authorization";
}
}
// Create a sample API with dotnet new webapi, then return an Authorization header
public ActionResult<IEnumerable<string>> Get()
{
Response.Headers.Add("Authorization", "Bearer dpmtest");
return Ok("");
}
@dustinsoftware
Copy link
Author

With dotnet version 2.1.300-rc1-008673:

NET Core 4.6.26426.02
System.Net.Http, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Headers.GetValues(Server): Kestrel
Headers.GetValues(Authorization): 
Headers.First(x => x.Key == Authorization): Bearer dpmtest

@dustinsoftware
Copy link
Author

With a .NET 4.7 app referencing a .NET Standard 2.0 library

System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Headers.GetValues(Server): Kestrel
Headers.GetValues(Authorization): Bearer dpmtest
Headers.First(x => x.Key == Authorization): Bearer dpmtest

@dustinsoftware
Copy link
Author

dustinsoftware commented Jul 18, 2018

With just a .NET 4.7 app:

System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Headers.GetValues(Server): Kestrel
Headers.GetValues(Authorization): Bearer dpmtest
Headers.First(x => x.Key == Authorization): Bearer dpmtest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment