문제

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
{
 ........
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top