[英]Retrieve JSON with StackOverflow in C#
我的问题非常简单,并且在代码注释中不言自明。 使用不同于StackOverflow的URL时,Json毫无问题地出现,但是,从StackOverflow获得的Json则具有奇怪的编码形式。
这是我的代码:
static void Main(string[] args)
{
//goodUrl is working in this program and in the browser.
string goodUrl = "http://yuml.me/23db58a4.json";
//badUrl is not working in this program, but works fine in the browser.
string badUrl = "https://api.stackexchange.com/2.1/me?key=qxtbTbIIEhAZFGO0QOziMA((&site=stackoverflow&order=desc&sort=reputation&access_token=mytoken&filter=!23IiyZnRyYmQ4bPZYWRA1";
HttpWebRequest webRequest = System.Net.WebRequest.Create(badUrl) as HttpWebRequest;
webRequest.Method = "GET";
webRequest.ServicePoint.Expect100Continue = false;
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
try
{
string str = responseReader.ReadToEnd();
//With Bad-url, "str" ENCODE JSON but with STRANGE ASCII CODE.
Console.Write(str);
Console.Read();
}
//...
}
真正的问题在哪里,我该如何解决?
您得到压缩的结果。
var stream = webRequest.GetResponse().GetResponseStream();
MemoryStream m = new MemoryStream();
new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress).CopyTo(m);
try
{
string str = Encoding.UTF8.GetString(m.ToArray());
Console.Write(str);
Console.Read();
}
finally
{
........
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.