Last active
July 18, 2018 16:46
-
-
Save dustinsoftware/424ba34074f4044a1420713bda37104a to your computer and use it in GitHub Desktop.
HttpClient headers repro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(""); | |
} |
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
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
With dotnet version 2.1.300-rc1-008673: