Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadString gives a timeout on a https url seeming to work in a browser

I am trying to get the content from this URL into my program: https://data.mtgox.com/api/2/BTCUSD/money/ticker . When visiting the URL in any of my browsers, it works perfectly.

However in my program, it waits for 90 seconds, and give me a timeout.

This is my code:

    private const string ApiLnk = "https://data.mtgox.com/api/2/BTCUSD/money/ticker";

    static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        {
            string s = client.DownloadString(ApiLnk);
            int i = 0;
        }
    }

The string s never gets assigned, as the client.DownloadString() is the one stalling.

When getting a normal URL like Google.com, it works perfectly.

Any idea what's wrong?

like image 777
Lars Holdgaard Avatar asked Dec 11 '22 16:12

Lars Holdgaard


2 Answers

Just set HttpRequestHeader.Accept and HttpRequestHeader.UserAgent headers. This works

using (WebClient client = new WebClient())
{
    client.Headers[HttpRequestHeader.Accept] = "text/html, image/png, image/jpeg, image/gif, */*;q=0.1";
    client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12";
    string s = client.DownloadString(ApiLnk);
    int i = 0;
}
like image 168
I4V Avatar answered Dec 14 '22 05:12

I4V


Add a user-agent to the headers and you should be fine.

client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
like image 29
scartag Avatar answered Dec 14 '22 07:12

scartag