【问题标题】:C# retrieve JSON from URLC# 从 URL 检索 JSON
【发布时间】:2016-02-10 18:14:25
【问题描述】:

我正在创建一个程序,用于获取各种 CS:GO 项目的 Steam 市场价格,并将它们相互比较。我无法将 Steam 市场 JSON 导入我的程序。 Market JSON 这是我的代码:

using (WebClient webClient = new System.Net.WebClient())
{
    WebClient n = new WebClient(); // <-- error on this line
    string json = n.DownloadString("http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=P250%20%7C%20Valence%20(Factory%20New");
    dynamic array = JsonConvert.DeserializeObject(json);
    test.Text = array.lowest_price.ToString(); ;
}

我在实例化 WebClient() 时收到此错误:

“System.Net.WebException”类型的未处理异常发生在 系统.dll

附加信息:远程服务器返回错误:(500) 内部服务器错误。

这甚至是一个有效的 JSON 吗?如果没有,是否有替代方案?我听说背包.tf 也有一个 api。这会更好吗? 谢谢

【问题讨论】:

  • 你为什么要实例化 WebClient 两次?
  • 哇哦。甚至没有注意到这一点。仍然会导致同样的错误
  • 如果您在异常窗口中单击查看详细信息,您应该能够在响应属性或内部异常中看到应该为您提供更多信息的信息。
  • 错误是“500 Internal Service”错误 - 这意味着您的客户正在联系的服务器上的服务出现问题。除非您有权访问该服务和服务器,否则您很可能无法进一步确定任何事情。您需要联系服务所有者。
  • 您在代码中的 URL 缺少结束括号 ')'

标签: c# json steam steam-web-api


【解决方案1】:

固定代码:

using (var webClient = new System.Net.WebClient())
{
    var json = webClient.DownloadString("http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=P250%20%7C%20Valence%20(Factory%20New)");
    dynamic array = JsonConvert.DeserializeObject(json);
    var price = array.lowest_price.ToString(); ;
}

【讨论】:

    【解决方案2】:

    该 URL 似乎格式不正确。我刚试了http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Shadow%20Case,得到了很好的回应。

    需要特别注意的是 URL 中的括号。

    【讨论】:

    • 这是网址,但 Steam 社区仍然无法使用。改用backpack.tf。
    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 2015-02-06
    • 2012-10-30
    • 2016-09-15
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多