gpt4 book ai didi

c# - HttpClient 抛出 System.ArgumentException : 'windows-1251' is not a supported encoding name

转载 作者:行者123 更新时间:2023-11-30 14:27:46 25 4
gpt4 key购买 nike

我正在编写 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);

关于c# - HttpClient 抛出 System.ArgumentException : 'windows-1251' is not a supported encoding name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31638035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com