Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data from url returns empty value

I have this code that creates a request and reads the data but it is always empty

        static string uri = "http://yiimp.ccminer.org/api/wallet?address=DshDF3zmCX9PUhafTAzxyQidwgdfLYJkBrd";

    static void Main(string[] args)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Get the stream associated with the response.
        Stream receiveStream = response.GetResponseStream();

        // Pipes the stream to a higher level stream reader with the required encoding format. 
        StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

        Console.WriteLine("Response stream received.");
        Console.WriteLine(readStream.ReadToEnd());
        response.Close();
        readStream.Close();

    }

When i try to access the link from browser i get this json:

{"currency": "DCR", "unsold": 0.030825917365192, "balance": 0.02007306, "unpaid": 0.05089898, "paid24h": 0.05796425, "total": 0.10886323}

what am I missing?

like image 395
Sas Gabriel Avatar asked Aug 31 '17 08:08

Sas Gabriel


Video Answer


1 Answers

When you perform the request from a browser there are a lot of headers that get sent to the web service. Apparently this web service validates the UserAgent. This is a decision on the part of the web service implementation, they might not want you to programmatically access it.

var client = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://yiimp.ccminer.org/api/wallet?address=DshDF3zmCX9PUhafTAzxyQidwgdfLYJkBrd"));
client.AutomaticDecompression = DecompressionMethods.GZip;
client.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063";
client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
client.Host = "yiimp.ccminer.org";
client.KeepAlive = true;

using (var s = client.GetResponse().GetResponseStream())
using (var sw = new StreamReader(s))
{

    var ss = sw.ReadToEnd();
    Console.WriteLine(ss);
}

Sending the headers seems to make it work.

like image 180
Titian Cernicova-Dragomir Avatar answered Oct 23 '22 00:10

Titian Cernicova-Dragomir