Dotnet webclient timesout but browser works file for json webservice

泪湿孤枕 提交于 2019-12-06 10:02:59

Hmm, strage, this works great for me:

class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
            var result = client.DownloadString("https://mtgox.com/code/data/getDepth.php");
            Console.WriteLine(result);
        }
    }
}

Notice that I am specifying a User Agent HTTP header as it seems that the site is expecting it.

I had similar issue before. request.KeepAlive = false solved my problem. Try this:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://mtgox.com/code/data/getDepth.php");
  request.KeepAlive = false;
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      {

           using (StreamReader sr = new StreamReader(response.GetResponseStream()))
           {
               string data = sr.ReadToEnd();
           }
       }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!