我试图通过url以JSON格式请求StackExchange上的标签列表,但问题是,我得到的是一些损坏的文本而不是JSON,所以我可以'甚至解析它。
P.S。在RestSharp的帮助下完成。
private void Refresh()
{
var client = new RestClient("http://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow");
var result = client.Execute(new RestRequest(Method.GET));
var array = JsonConvert.DeserializeObject<Root>(result.Content);
Platforms = array.Platforms;
}
答案 0 :(得分:2)
如果您使用Fiddler向此网址发出GET请求,您会看到该回复有一个标题:
Content-Encoding: gzip
这意味着使用gzip压缩响应。好消息是HttpWebRequest
可以处理:
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
添加此行后,您将获得漂亮且可读的JSON。
答案 1 :(得分:0)
正如@peeskillet所提到的,这看起来像压缩数据。请看看 What is the canonical method for an HTTP client to instruct an HTTP server to disable gzip responses?,尤其是this answer。
像
这样的东西Accept-Encoding: *;q=0
应该有帮助。