WP7:编码。默认
我正在尝试从 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);
}
}
重要的部分是 Encoding.Default。如果我选择其他任何内容,它将返回错误 GZIP 标头,第一个魔术字节不匹配'或类似的内容。
WP7没有默认值,它只有Unicode和UTF8,但它们都不起作用。
有想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用 WebClient.DownloadString,而使用 DownloadData。这样您将收到 GZip 编码的字节(无法真正转换为字符串),并且可以将其直接传递到 GZupInputStream。
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.
请改用 WebRequest.BeginGetResponse。这样你就可以按照@carlosfigueria的建议获取字节,但由于webclient只有getstring,这是一个解决方法。
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.