我在WP7应用程序中尝试从SO API中获取结果。当我使用以下代码时,我能够在控制台应用程序中工作

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);
            }

        }
.

重要零件是编码.Default。如果我选择任何其他东西,它会用错误gzip标题回来,第一个魔法字节不匹配'或类似的东西。

wp7不默认,它只有Unicode和UTF8,它们都没有工作。

想法?

有帮助吗?

解决方案 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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top