我建立一个Windows应用商店的应用程序,但我被困在得到从API使用UTF-8的反应。
I'm building a Windows Store app, but I'm stuck at getting a UTF-8 response from an API.
这是在code:
using (HttpClient client = new HttpClient())
{
Uri url = new Uri(BaseUrl + "/me/lists");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("Accept", "application/json");
HttpResponseMessage response = await client.SendRequestAsync(request);
response.EnsureSuccessStatusCode();
string responseString = await response.Content.ReadAsStringAsync();
response.Dispose();
}
在 reponseString
总是包含奇怪的字符,这应该是口音一样的电子的,我试图使用流,但是API,我发现在一些例子并不能在Windows RT存在。
The reponseString
always contains strange characters which should be accents like é, and I tried using a stream, but the API I found in some examples don't exist in Windows RT.
编辑:提高code,还是同样的问题
edit: improved code, still same problem
使用,而不是 response.Content.ReadAsStringAsync()
直接可以使用 response.Content.ReadAsBufferAsync()
通过@Kiewic指出如下:
Instead of using response.Content.ReadAsStringAsync()
directly you could use response.Content.ReadAsBufferAsync()
pointed by @Kiewic as follows:
var buffer = await response.Content.ReadAsBufferAsync();
var byteArray = buffer.ToArray();
var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
这是工作在我的情况,我想这使用UTF8要解决大部分的问题。现在去的身影,为什么没有办法使用要做到这一点 ReadAsStringAsync
:)
This is working in my case and I guess that using UTF8 should solve most of the issues. Now go figure why there is no way to do this using ReadAsStringAsync
:)