我正在尝试获取以下 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/