Retrieve data via JSON by URL in C#

Asked

Viewed 155 times

3

I need to get data via JSON, through a URL, this link: https://cryptohub.online/api/market/ticker/egx/

I’m trying with the code below, but I already get error as soon as it downloads the string.

The error received is:

The character chair is not in JSON format.

However, by the (little) I understand, it seems to me that the return is OK in format.

Any idea what might be going on?

This is my code:

using (var webClient = new System.Net.WebClient())
        {
            var json = webClient.DownloadString("https://cryptohub.online/api/market/ticker/EGX/");

            dynamic obj = JsonConvert.DeserializeObject(json);

            var btc = 0.0;


                foreach (var result in obj.BTC_EGX)

                btc = result.last;

1 answer

3


Hello! There was an update in the TLS protocol to 1.2 so https stopped working because they were set to the old TLS 1.0. Arrow the System.Net Secutiry Protocol as I added in your example below. abs

using (WebClient webClient = new System.Net.WebClient())
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            var json = webClient.DownloadString("https://cryptohub.online/api/market/ticker/EGX/");

            dynamic obj = JsonConvert.DeserializeObject(json);

            var btc = 0.0;


            foreach (var result in obj.BTC_EGX)

                btc = result.last;

        }
  • Hi Paulrocost, gave error when compiling: Error The name 'Servicepointmanager' does not exist in the current context

  • tomorrow we’ll come back to try to settle this here, anyway, thanks for the force!

  • 2

    you just need to add the reference, the using, to the ServicePoint Manager. If I’m not mistaken, it’s: using System.Net;.

  • 1

    That’s right; you need to add the reference "using System.Net" because the object you will set tls is inside it.

  • Perfect Solved the problem! Thank you @paulocost and vhoyer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.