WP7: codificación.default
-
14-11-2019 - |
Pregunta
Estoy tratando de obtener resultados de SO API en una aplicación WP7.Pude ponerlo en una aplicación de consola cuando usé el siguiente código
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);
}
}
La parte importante fue la codificación.Si elegí algo más, volvería con el encabezado GZIP ERROR, el primer byte mágico no coincide "o algo similar.
wp7 no tiene predeterminado, solo tiene unicode y utf8 que ninguno de ellos funciona.
ideas?
Solución 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.
Otros consejos
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.