HttpClient 抛出 System.ArgumentException: 'windows-1251' 不是受支持的编码名称
HttpClient throws System.ArgumentException: 'windows-1251' is not a supported encoding name
我正在编写 WinPhone 8.1 应用程序。
代码非常简单并且适用于大多数情况:
string htmlContent;
using (var client = new HttpClient())
{
htmlContent = await client.GetStringAsync(GenerateUri());
}
_htmlDocument.LoadHtml(htmlContent);
但有时
会抛出异常
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) at
System.Globalization.EncodingTable.GetCodePageFromName(String name)
at
System.Net.Http.HttpContent.<>c__DisplayClass1.b__0(Task
task)} System.Exception {System.ArgumentException}
HttpClient支持1251编码吗?如果没有,我该如何避免这个问题?还是目标页面的问题?还是我哪里错了?
获取响应 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);
我正在编写 WinPhone 8.1 应用程序。 代码非常简单并且适用于大多数情况:
string htmlContent;
using (var client = new HttpClient())
{
htmlContent = await client.GetStringAsync(GenerateUri());
}
_htmlDocument.LoadHtml(htmlContent);
但有时
会抛出异常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) at System.Globalization.EncodingTable.GetCodePageFromName(String name)
at System.Net.Http.HttpContent.<>c__DisplayClass1.b__0(Task task)} System.Exception {System.ArgumentException}
HttpClient支持1251编码吗?如果没有,我该如何避免这个问题?还是目标页面的问题?还是我哪里错了?
获取响应 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);