我正在制作 uwp(通用 Windows 平台)应用程序并想反序列化此 xml:http://radioa24.info/ramowka.php反对,但我得到的是特殊字符,如 ł、ó 一些奇怪的字母和特殊字母,如:\n 和\r:
“Ä…”=>“±”
“ć”=>“ć”
“Ä™”=>“ę”
例如,我得到的不是 Poniedziałek
PoniedziaÅ\u0082ek
我的代码:
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(uri).AsTask();
response.EnsureSuccessStatusCode();
var result = await httpResponse.Content.ReadAsStringAsync();
我试图进行一些编码转换,但没有成功。 如何解决它,因为以后我想得到我的对象?
var reader = new XmlSerializer(typeof(Sources.Schedule));
using (var tr = new MemoryStream(Encoding.UTF8.GetBytes(resultString)))
{
Schedule = (Sources.Schedule)reader.Deserialize(res);
}
最佳答案
请试试下面的代码,以字节读取数据解决了你的问题。
using (HttpClient client = new HttpClient())
{
Uri url = new Uri("http://radioa24.info/ramowka.php");
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
Task<HttpResponseMessage> responseAsync = client.SendRequestAsync(httpRequest).AsTask();
responseAsync.Wait();
responseAsync.Result.EnsureSuccessStatusCode();
Task<IBuffer> asyncBuffer = responseAsync.Result.Content.ReadAsBufferAsync().AsTask();
asyncBuffer.Wait();
byte[] resultByteArray = asyncBuffer.Result.ToArray();
string responseString = Encoding.UTF8.GetString(resultByteArray, 0, resultByteArray.Length);
responseAsync.Result.Dispose();
}
关于c# - 在 UWP 中接收来自 HttpClient 的响应时编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35048398/