diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index a0ec902fb96..df1ef750529 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -84,7 +84,7 @@ internal override void ProcessResponse(HttpResponseMessage response) if (ShouldWriteToPipeline) { - using var responseStream = new BufferingStreamReader(baseResponseStream, _cancelToken.Token); + using BufferingStreamReader responseStream = new(baseResponseStream, _cancelToken.Token); // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overridden it with a return type of "XML" @@ -94,37 +94,36 @@ internal override void ProcessResponse(HttpResponseMessage response) } else { - // Determine the response type - RestReturnType returnType = CheckReturnType(response); - // Try to get the response encoding from the ContentType header. string? characterSet = WebResponseHelper.GetCharacterSet(response); string str = StreamHelper.DecodeStream(responseStream, characterSet, out Encoding encoding, _cancelToken.Token); - object? obj = null; - Exception? ex = null; - string encodingVerboseName; try { - encodingVerboseName = string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName; + encodingVerboseName = encoding.HeaderName; } - catch (NotSupportedException) + catch { - encodingVerboseName = encoding.EncodingName; + encodingVerboseName = string.Empty; } // NOTE: Tests use this verbose output to verify the encoding. WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encodingVerboseName}")); + // Determine the response type + RestReturnType returnType = CheckReturnType(response); + bool convertSuccess = false; + object? obj = null; + Exception? ex = null; if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } - // default to try xml first since it's more common + // Default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); @@ -132,7 +131,7 @@ internal override void ProcessResponse(HttpResponseMessage response) if (!convertSuccess) { - // fallback to string + // Fallback to string obj = str; } @@ -171,11 +170,8 @@ private static RestReturnType CheckReturnType(HttpResponseMessage response) RestReturnType rt = RestReturnType.Detect; string? contentType = ContentHelper.GetContentType(response); - if (string.IsNullOrEmpty(contentType)) - { - rt = RestReturnType.Detect; - } - else if (ContentHelper.IsJson(contentType)) + + if (ContentHelper.IsJson(contentType)) { rt = RestReturnType.Json; } @@ -272,7 +268,7 @@ private static bool TryConvertToXml(string xml, [NotNullWhen(true)] out object? XmlReaderSettings settings = GetSecureXmlReaderSettings(); XmlReader xmlReader = XmlReader.Create(new StringReader(xml), settings); - var xmlDoc = new XmlDocument(); + XmlDocument xmlDoc = new(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load(xmlReader);