HttpClient抛出System.ArgumentException: & # 39; windows - 1251
本文关键字:windows 1251 抛出 System ArgumentException HttpClient | 更新日期: 2023-09-27 18:04:48
我正在写WinPhone 8.1应用程序。代码非常简单,在大多数情况下都可以工作:
string htmlContent;
using (var client = new HttpClient())
{
htmlContent = await client.GetStringAsync(GenerateUri());
}
_htmlDocument.LoadHtml(htmlContent);
但是有时会在
抛出异常htmlContent = await client.GetStringAsync(GenerateUri());
HttpClient是否支持1251编码?如果没有,我怎样才能避免这个问题呢?还是目标页面问题?还是我哪里错了?InnerException{系统。ArgumentException: 'windows-1251'不是支持的编码名称。参数名称:名称atSystem.Globalization.EncodingTable.internalGetCodePageFromName(字符串的名字)System.Globalization.EncodingTable。GetCodePageFromName(字符串名称)
在System.Net.Http.HttpContent灵活;> c__DisplayClass1.b__0(任务任务)}系统。异常{系统。ArgumentException}
获取响应为IBuffer
,然后使用。net编码类进行转换:
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);