コード例 #1
0
        private async Task DumpContent(HttpContent content)
        {
            if (content == null || content.Headers.ContentType == null)
            {
                return;
            }

            var result = await content.ReadAndDecodeAsStringAsync();

            if (content.Headers.ContentType.MediaType.Contains(Constants.JsonContentType))
            {
                try
                {
                    if (result.StartsWith("["))
                    {
                        Program.PrintColoredJson(JArray.Parse(result));
                        return;
                    }
                    else if (result.StartsWith("{"))
                    {
                        Program.PrintColoredJson(JObject.Parse(result));
                        return;
                    }
                }
                catch (Exception)
                {
                    // best effort
                }
            }
            else if (content.Headers.ContentType.MediaType.Contains(Constants.XmlContentType) ||
                     content.Headers.ContentType.MediaType.Contains("application/xml"))
            {
                try
                {
                    Program.PrintColoredXml(XDocument.Parse(result).ToString());
                    return;
                }
                catch (Exception)
                {
                    // best effort
                }
            }

            Console.Write(result);
        }