DOTNET Web客户端timesout但浏览器的工作原理文件JSON web服务 [英] Dotnet webclient timesout but browser works file for json webservice
问题描述
我试图让下面的JSON web服务 https://开头mtgox .COM / code /数据/ getDepth.php 的成字符串使用下面的code。
I am trying to get the result of the following json webservice https://mtgox.com/code/data/getDepth.php into a string using the following code.
using (WebClient client = new WebClient())
{
string data = client.DownloadString("https://mtgox.com/code/data/getDepth.php");
}
但它总是返回超时异常,并没有数据。我打算使用FASTJSON开启响应成对象并预期是网页的内容的硬部分未返回的
but it always returns a timeout exception and no data. I plan to use fastjson to turn the response into objects and expected that to be that hard part not the returning of the content of the page.
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();
}
}
也导致同样的错误。任何人都可以指出我在做什么错了?
Also resulted in the same error. Can anyone point out what i am doing wrong?
推荐答案
嗯,strage,这工作非常适合我:
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);
}
}
}
请注意,我指定一个用户代理HTTP头,因为它看来,该网站是预料到的。
Notice that I am specifying a User Agent HTTP header as it seems that the site is expecting it.
这篇关于DOTNET Web客户端timesout但浏览器的工作原理文件JSON web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!