My problem is very simple and self-explanatory in the comments of the code. With a url different from StackOverflow, the Json appears with no problem but, with a Json obtained from StackOverflow it has a strange encoded form.
Here is my code:
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();
}
//...
}
Where is the real problem and how can I solve this?
You get a compressed result.
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
{
........
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.