0

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?

5
  • 1
    Could you give an example of the 'strange encoded form'? Commented Feb 10, 2014 at 15:52
  • 3
    You're probably seeing a gzipped response. Commented Feb 10, 2014 at 15:53
  • One thing I will suggest is to read the response line by line and not ReadToEnd because there is a chance that the file will be pretty big. Commented Feb 10, 2014 at 15:54
  • @JohnOdom: Since he needs to parse the JOSN anyway, that won't help much. He should actually pass the stream directly to the parser. Commented Feb 10, 2014 at 15:55
  • Well. You can see that the JSON is very small if click on that link. The strange encode matches with the length of Json. here what I got: imgur.com/572yB6A Commented Feb 10, 2014 at 15:55

1 Answer 1

1

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
{
 ........
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah! It worked for me! I got a compressed result. Many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.