Skip to content

Instantly share code, notes, and snippets.

@dustinsoftware dustinsoftware/client.cs
Last active Jul 18, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

dustinsoftware commented Jul 18, 2018

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

This comment has been minimized.

Copy link
Owner Author

dustinsoftware commented Jul 18, 2018

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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.