Dotnet webclient超时但浏览器为json webservice工作文件

我试图使用以下代码将以下json webservice https://mtgox.com/code/data/getDepth.php的结果转换为字符串。

using (WebClient client = new WebClient()) { string data = client.DownloadString("https://mtgox.com/code/data/getDepth.php"); } 

但它总是返回超时exception而没有数据。 我计划使用fastjson将响应转换为对象,并期望这是一个困难的部分而不是返回页面的内容。

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://mtgox.com/code/data/getDepth.php"); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { string data = sr.ReadToEnd(); } } 

也导致了同样的错误。 谁能指出我做错了什么?

嗯,strage,这对我很有用:

 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); } } } 

请注意,我正在指定用户代理HTTP标头,因为该网站似乎正在期待它。

我之前有类似的问题。 request.KeepAlive = false解决了我的问题。 试试这个:

  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(); } }