diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs index ec570876343..fbcbdd80181 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs @@ -4,6 +4,8 @@ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ +using System; +using System.Collections.Generic; using System.Net.Http; using System.Globalization; @@ -16,6 +18,28 @@ internal static string GetCharacterSet(HttpResponseMessage response) string characterSet = response.Content.Headers.ContentType.CharSet; return characterSet; } + + internal static Dictionary> GetHeadersDictionary(HttpResponseMessage response) + { + var headers = new Dictionary>(StringComparer.OrdinalIgnoreCase); + foreach (var entry in response.Headers) + { + headers[entry.Key] = entry.Value; + } + // In CoreFX, HttpResponseMessage separates content related headers, such as Content-Type to + // HttpResponseMessage.Content.Headers. The remaining headers are in HttpResponseMessage.Headers. + // The keys in both should be unique with no duplicates between them. + // Added for backwards compatibility with PowerShell 5.1 and earlier. + if (response.Content != null) + { + foreach (var entry in response.Content.Headers) + { + headers[entry.Key] = entry.Value; + } + } + + return headers; + } internal static string GetProtocol(HttpResponseMessage response) { diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs index 9e4b52bfd0f..d683a833b4b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs @@ -31,23 +31,17 @@ public Dictionary> Headers { get { - var headers = new Dictionary>(StringComparer.OrdinalIgnoreCase); - foreach (var entry in BaseResponse.Headers) + if(_headers == null) { - headers[entry.Key] = entry.Value; - } - if (BaseResponse.Content != null) - { - foreach (var entry in BaseResponse.Content.Headers) - { - headers[entry.Key] = entry.Value; - } + _headers = WebResponseHelper.GetHeadersDictionary(BaseResponse); } - return headers; + return _headers; } } + private Dictionary> _headers = null; + /// /// gets the RelationLink property ///