getting json data from url in c#

3.6k Views Asked by At

I'm trying to get json data from the below url in to a C# project into anything, before I can parse/use the data in a chart:

https://cex.io/api/ohlcv/hd/20160228/BTC/USD

I was actually retrieving the json data by sendin json requests with a websocket but for some reason couldn't get any data with this.So I decided to get the data without providing any json request, as the link was simple.But whatever I tried I ended up with either empty returns or error.At one point I've managed to add a huge chunk of data to the ListBox(seemed like a full html page with some data I needed but I couldn't understand where the whole html code also came from as the link only has json) but that was useless as well. When tried

WebRequest with HttpWebRequest it didn't recognize WebRequest even with the correct system.net.http and all the rest included.(tried to add manually to References..but they were already checked on the list) WebClient returns empty or useless object or variable type names. HttpClient also didn't work but can't remember what it didn't recognize. When I hit the rock bottom I even tried to put a webBrowser object and tried to read it from there but then noticed that this shouldn't be that hard, and decided to ask.

Here is what I have done so far:

  //HttpClient client3 = new HttpClient();
                             string url = textBox8.Text + listBox3.GetItemText(listBox3.SelectedItem) + listBox4.GetItemText(listBox4.SelectedItem);

                             MessageBox.Show(url);


                             //System.Uri uri = new System.Uri(url);
                             //webBrowser1.Url = uri;

                             System.Net.WebClient client8 = new System.Net.WebClient();
                             //client8.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"); //I dont know if this was needed but seen it on a different example and just added.
                             var html = client8.DownloadString(url);   //returns empty
                             var html2 = client8.DownloadData(url); //adds the line "System.Byte[]"
                             var html3 = client8.OpenRead(url); //adds something like "System.blabla.connectstream"
                             MessageBox.Show(html);


                             listBox1.Items.Add(html2.ToString());
                             listBox1.Items.Add(html3);

I'd appreciate if someone could help.

2

There are 2 best solutions below

5
On BEST ANSWER

@berkdi, you are right. MessageBox.Show shows an empty string. But in fact, your html string is correct and you can see it by writing the content to a file like File.WriteAllText("myfile.txt", html);.

Below code (when displaying the first 3000 chars of your text) works too

System.Net.WebClient client8 = new System.Net.WebClient();
var html = client8.DownloadString("https://cex.io/api/ohlcv/hd/20160228/BTC/USD");
Console.WriteLine(html);
MessageBox.Show(html.Substring(0, 3000));

Sorry, You hit a bug/strange behaviour/bad implementation of MessageBox.Show

EDIT

This seems to be the common problem of windows controls that are not designed to display a very long multiline data...

2
On

Using WebClient is fine

var client = new System.Net.WebClient();
var jsondata = client.DownloadString(url);

Will do what you want. I have tested it with the url provided, and it does not return an empty string, but the json data. I think that you might have mangled the url a bit when creating it from your input controls. Can you verify that the url used is actually the same url that you have posted here? Perhaps you are missing an / between the last two strings used for creating the url