Home > Mobile >  Reading Json with C# via HTTP Request
Reading Json with C# via HTTP Request

Time:11-05

I have a problem reading json from our local API.

This is a well known public json side which I also used for testing: enter image description here

In C# I tried this:

        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.

The json from the public json side works but my local not.

This is my output in the Console: enter image description here

What can I do about it? Thanks in Advance!

CodePudding user response:

Try adding your accept header

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

CodePudding user response:

SOLUTION:

My problem was gzip it compressed my 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();
            }
  • Related