不是受支持的编码名称

不是受支持的编码名称

本文介绍了'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:

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'不是受支持的编码名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:08