Question

I'm trying to get results from SO api in a WP7 app. I was able to get it working in a console app when I used the following code

static void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            Console.Clear();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
            var stream = new MemoryStream(Encoding.Default.GetBytes(e.Result));
            var gzstream = new GZipInputStream(stream);

            RootObject qs = ser.ReadObject(gzstream) as RootObject;

            foreach (Question q in qs.questions)
            {
                Console.WriteLine(q.title);
            }

        }

the important part was Encoding.Default. If I chose anything else it would come back with Error GZIP header, first magic byte doesn't match' or something similar.

WP7 doesnt have default, it only has Unicode and UTF8 which neither of them work.

Ideas?

Was it helpful?

Solution 2

use WebRequest.BeginGetResponse instead. This way you can get the bytes as @carlosfigueria suggested but since webclient only has getstring this is a work around.

OTHER TIPS

Don't use WebClient.DownloadString, use DownloadData. This way you'll receive the GZip-encoded bytes (which can't really be converted to string), and you can pass it directly to the GZupInputStream.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top