'UTF8'不是受支持的编码名称 [英] 'UTF8' is not a supported encoding name

查看:1187
本文介绍了'UTF8'不是受支持的编码名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我只是在使用Spotify的Web API,并且正在尝试访问播放次数最多的曲目.尽管我遇到了问题,但现在已经尝试解决了几个小时,但找不到答案.

So I'm just playing around with Spotify's Web API and I'm trying to access my top played tracks. Although I've encountered a problem I've been trying to solve for a couple of hours now but I can't find an answer.

当我尝试反序列化响应时,出现以下错误:

When I try to deserialize my response, I get the follwing error:

'UTF8'不是受支持的编码名称.有关定义自定义编码的信息,请参见Encoding.RegisterProvider方法的文档. 参数名称:名称 ContentType中提供的字符集无效.无法使用无效的字符集将内容读取为字符串.

'UTF8' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Parameter name: name The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.

ContentType为application/json; charset=UTF8

The ContentType is application/json; charset=UTF8

有什么想法吗?

这是我的请求代码:

private static HttpClient GetHttpClient()
{
    HttpClientHandler handler = new HttpClientHandler() {
        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
    };
    var httpClient = new HttpClient(handler);
    httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
    return httpClient;
}

public async Task<SearchArtistResponse> GetSelfTopAsync(string type, string userName)
{
    var httpClient = GetHttpClient();
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetAccessToken(userName));

    var sb = new StringBuilder();
    sb.Append(ApiUrl);
    sb.Append($"/me/top/{type}");
    var query = sb.ToString();
    var response = await httpClient.GetAsync(query);

    var spotifyResponse = JsonConvert.DeserializeObject<SearchArtistResponse>(await response.Content.ReadAsStringAsync());
    return spotifyResponse;
}

推荐答案

您是否正在使用.net core?

Are you using .net core?

您将需要添加以下代码,以使.NET桌面中的编码在您的环境中可用:

You will need to add the following code to make the encodings available in .NET desktop available in your environment:

System.Text.EncodingProvider provider = System.Text.CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(provider);

有关CodePagesEncodingProvider.Instance的更多信息,请参见此处.

More info on CodePagesEncodingProvider.Instance can be found here.

这篇关于'UTF8'不是受支持的编码名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆