c# - HTTP 响应中的无效字符集

标签 c# .net http

我正在为 Windows Phone 8.1 编写 C# 应用程序。应用程序向带有post数据的网页发送http请求,但是,当我将请求内容复制到字符串时,它会抛出异常。

这是代码,

var values = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("username", "usernameHere"),
        new KeyValuePair<string, string>("password", "passwordHere"),
    };

var httpClient = new HttpClient(new HttpClientHandler());
var response = await httpClient.PostAsync(new Uri(LOGIN_URL), new FormUrlEncodedContent(values));

if (response.IsSuccessStatusCode)
{
    string responseString = "";

    try
    {
        responseString = await response.Content.ReadAsStringAsync();
    }

    catch (Exception e)
    {
        MessageBox.Show("Exception caught " + e);
    }
}

错误是,

"System.InvalidOperationException: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set. ---> System.ArgumentException: 'ISO8859_1' is not a supported encoding name."

显然,解决方案是使用 GetByteArrayAsync 而不是 PostAsync ( How to change the encoding of the HttpClient response ),但这样我无法提交发布数据。

最佳答案

Apparently the solution is to use GetByteArrayAsync instead of PostAsync

不,您可以使用 HttpContentReadAsByteArrayAsync 方法类

byte[] data = await response.Content.ReadAsByteArrayAsync();

关于c# - HTTP 响应中的无效字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29451576/

相关文章:

c# - 编写代码以触发最后一个方法以在多线程 Web 应用程序中抛出异常

c# - 在 LINQ 中比较过去的日期

c# - Msmq .NET api MessageQueue.Send 不会在预期的地方抛出

c# - 如何解决 Entity Framework 中的内存不足异常错误?

xml - Apache Camel - 向 http 端点发送 xml 参数不起作用

c# - 如何检查我的数组中是否有重复值?

c# - 获取在列表中出现两次的对象列表

c# - ASP.NET 将所有文件下载为 Zip

http 代理 heroku 登录 enotfound : getaddrinfoo enotfound api. heroku.com:443

python - 终极吉他API和终极API的使用