c# - Dotnet webclient 超时,但浏览器可以处理 json web 服务的文件

标签 c# .net json webclient

我正在尝试获取以下 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");
}

但是总是返回超时异常,没有数据。我计划使用 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();
       }
   }

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

最佳答案

嗯,stage,这对我来说非常有用:

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 header ,因为站点似乎需要它。

关于c# - Dotnet webclient 超时,但浏览器可以处理 json web 服务的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6602990/

相关文章:

c# - 如何知道我的 linq 查询是否返回 null

javascript - 将 JS 文件加载到变量中

javascript - 如何对包含 HTML 字符的 JSON 字符串进行编码?

c# - 文本冒险中的编码交互

c# - 将列表打印为矩阵

python - 通过curl/urllib2向Pyramid应用程序发送json数据没有给出正确的请求。POST

c# - 在 C# 中创建一个 xml 文档并将该文件存储到项目的 bin 文件夹中

C# 表达式按关键字段对通用查询进行排序

c# - 如何获取有关异常的更多信息

c# - 将具有两列的 SQL IN 子句转换为 LINQ