gpt4 book ai didi

c# - Dotnet webclient 超时,但浏览器为 json web 服务工作文件

转载 作者:行者123 更新时间:2023-11-30 22:37:12 24 4
gpt4 key购买 nike

我正在尝试获取以下 json 网络服务的结果 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();
}
}

同样报错。谁能指出我做错了什么?

最佳答案

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com