HttpClient throws System.ArgumentException: "windows-1251" does not support encoding name
I am writing a WinPhone 8.1 application. The code is very simple and works in most cases:
string htmlContent;
using (var client = new HttpClient())
{
htmlContent = await client.GetStringAsync(GenerateUri());
}
_htmlDocument.LoadHtml(htmlContent);
But sometimes an exception is thrown on
htmlContent = await client.GetStringAsync(GenerateUri());
InnerException {System.ArgumentException: "windows-1251" is not a supported encoding name. Parameter name: name at System.Globalization.EncodingTable.internalGetCodePageFromName (String name) in System.Globalization.EncodingTable.GetCodePageFromName (string name)
in System.Net.Http.HttpContent <. > C__DisplayClass1.b__0 (Task task)} System.Exception {System.ArgumentException}
Does HttpClient 1251 support encoding? And if this is not the case, how can I avoid this problem? Or is it a landing page issue? Or am I doing something wrong?
source to share
Get the answer like IBuffer
and then do the conversion using .NET encoding classes:
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(uri);
IBuffer buffer = await response.Content.ReadAsBufferAsync();
byte[] bytes = buffer.ToArray();
Encoding encoding = Encoding.GetEncoding("windows-1251");
string responseString = encoding.GetString(bytes, 0, bytes.Length);
source to share