2016-01-27 52 views
2

我正在制作uwp(通用Windows平台)应用程序,并且想要反序列化此xml:http://radioa24.info/ramowka.php以反对,但我取而代之为特殊字符,例如某些奇怪的字母和特殊字符像:\ n和\ R: “一个......”=> “A” 以 “第‡”=> “C” “加上™”=> “E” 例如,而不是Poniedziałek我得到PoniedziaÅ\u0082ek在UWP中收到来自HttpClient的响应时出现错误的编码

我的代码:

var httpClient = new HttpClient(); 
var response = await httpClient.GetAsync(uri).AsTask(); 
response.EnsureSuccessStatusCode(); 
var result = await httpResponse.Content.ReadAsStringAsync(); 

我正在试图mak e一些编码转换,但没有解决。 如何解决它,因为后来我想获得我的对象?

var reader = new XmlSerializer(typeof(Sources.Schedule)); 
using (var tr = new MemoryStream(Encoding.UTF8.GetBytes(resultString))) 
{ 
    Schedule = (Sources.Schedule)reader.Deserialize(res); 
} 
+0

确定http://radioa24.info/ramowka.php 是UTF-8编码的 反应? –

+0

我也有这个问题。我的代码在.NET Core中工作正常。我建议在.NET Core中尝试你的代码片段。我敢打赌它会起作用。我认为这是UWP的一个问题。 –

+0

这让我疯狂!我能够使用UWP中的大多数REST API,但是这一个REST API不起作用。为什么? –

回答

1

这是我的例子,它适用于波兰语单词。

方法从页面中获取XML:

public async Task<string> GetXMl(string uri) 
    { 
     string result = null; 
     using (HttpClient httpClient = new HttpClient()) 
     { 
      var response = await httpClient.GetAsync(uri); 
      result = await response.Content.ReadAsStringAsync(); 
     } 
     return result; 
    } 

方法反序列化XML:

public void DeserializeXml(string xml) 
    { 
     var serializer = new XmlSerializer(typeof(ramowka)); 
     var buffer = Encoding.UTF8.GetBytes(xml); 
     using (var stream = new MemoryStream(buffer)) 
     { 
      var ramowka = (ramowka)serializer.Deserialize(stream); 
     } 
    } 

示例如何使用按钮单击事件的方法,例如:

private async void Button_Click(object sender, RoutedEventArgs e) 
    { 
     string xml = await GetXMl("http://radioa24.info/ramowka.php"); 
     DeserializeXml(xml); 
    } 

同样在这里,您将通过Visual Studio xml转换为C#类 http://pastebin.com/aJ4B1aCF

+0

谢谢你。我实际上有几乎相同的C#类 – Levvy

1

正如Jon Skeet在his answer中注意到的,这应该在服务器上修复。如果检查服务器返回以下Content-Type头:

Content-Type: text/xml 

它应该告诉你的编码(Content-Type: text/xml; charset=utf-8),这就是为什么它应该是一个服务器修复。

但如果你确信这是UTF-8(它是,因为响应XML包含<?xml version="1.0" encoding="UTF-8"?>),你可以这样做:

var httpClient = new HttpClient(); 
var response = await httpClient.GetBufferAsync(uri); 
var bytes = response.ToArray(); 
var properEncodedString = Encoding.UTF8.GetString(bytes); 
+0

我会问有此脚本的朋友解决这个问题。 ;) – Levvy

2

请你可以试试下面的代码,读数据字节解决了你的问题。

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(); 
} 
+0

希望它工作! – Levvy

+0

我测试了这两种方法,并看到了错误和正确的字符。对于这个方法responseString非常好。希望在你的应用中也能起作用。 @Levvy –