我的问题在代码的注释中非常简单且不言自明。 使用与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();
}
//...
}
真正的问题在哪里,我该如何解决?
答案 0 :(得分:1)
您获得了压缩结果。
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
{
........
}