简体   繁体   English

通过 HTTP 请求使用 C# 读取 Json

[英]Reading Json with C# via HTTP Request

I have a problem reading json from our local API.我在从我们的本地 API 读取 json 时遇到问题。

This is a well known public json side which I also used for testing: https://jsonplaceholder.typicode.com/todos这是一个众所周知的公共 json 端,我也用于测试: https : //jsonplaceholder.typicode.com/todos

This is my Json on my local API:这是我本地 API 上的 Json: 在此处输入图片说明

In C# I tried this:在 C# 我试过这个:

        HttpClient client = new HttpClient();

        string httpResponse = "";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(testurl);
        WebResponse responseinger = null;
        StreamReader reader = null;

        static async Task Main(string[] args)
        {
            Program p = new Program();
            await p.Get();
        }

        public async Task Get()
        {
            string response = await client.GetStringAsync("https://jsonplaceholder.typicode.com/todos");
            Console.WriteLine(response);
            Console.WriteLine();

            string response2 = await client.GetStringAsync(testurl);
            Console.WriteLine(response2);
            Console.WriteLine();

            string json = JsonConvert.SerializeObject(response2);
            Console.WriteLine(json);
            Console.WriteLine();

           
            try
            {
                responseinger = request.GetResponse();
            }
            catch (WebException ex)
            {
                responseinger = ex.Response;
            }

            reader = new StreamReader(responseinger.GetResponseStream());
            httpResponse = reader.ReadToEnd();

            Console.WriteLine(reader);
            Console.WriteLine();
            Console.WriteLine(httpResponse);
            Console.WriteLine();

            using (WebClient wc = new WebClient())
            {
                var jsonwc = wc.DownloadString(testurl);
                Console.WriteLine(jsonwc);
                Console.WriteLine();
            }
        }

So I used different methods to get my json.所以我使用了不同的方法来获取我的 json。

The json from the public json side works but my local not.来自公共 json 端的 json 有效,但我的本地无效。

This is my output in the Console:这是我在控制台中的输出: 在此处输入图片说明

What can I do about it?我该怎么办? Thanks in Advance!提前致谢!

Try adding your accept header尝试添加您的接受标头

    var req = WebRequest.CreateHttp(uri);              
    req.Headers.Add(HttpRequestHeader.Accept, "application/ json");
    req.Method = "Get";

SOLUTION:解决方案:

My problem was gzip it compressed my json.我的问题是 gzip 它压缩了我的 json。

            using (WebClient wc = new WebClient())
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(testurl);

                req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

                responseinger = req.GetResponse();

                reader = new StreamReader(responseinger.GetResponseStream());
                httpResponse = reader.ReadToEnd();

                //Console.WriteLine(reader);
                //Console.WriteLine();
                Console.WriteLine(httpResponse);
                Console.WriteLine();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM