2011-07-06 68 views
2

我想使用下面的代码将下面的JSON Web服务https://mtgox.com/code/data/getDepth.php的结果转换为字符串。Dotnet Web客户端超时,但浏览器工作文件为JSON Web服务

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

但它总是返回一个超时异常并且没有数据。我打算使用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(); 
     } 
    } 

也造成了同样的错误。任何人都可以指出我做错了什么?

回答

3

嗯,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标头,因为它似乎该网站期待它。

+0

非常感谢。不能相信我浪费了很多时间来填充所有的设置,并且这是整个浏览器设置的时间。我应该早点问。感谢您的快速回复 – Seer

0

我以前有类似的问题。 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(); 
      } 
     } 
+0

对不起在您工作之前测试过其他帖子 – Seer