因此,我只是在使用Spotify的Web API,并且正在尝试访问播放次数最多的曲目。尽管我遇到了问题,但现在已经尝试解决了几个小时,但找不到答案。
当我尝试反序列化响应时,出现以下错误:
ContentType是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?
您将需要添加以下代码,以使.NET桌面中的编码在您的环境中可用:
System.Text.EncodingProvider provider = System.Text.CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(provider);
可以在here中找到有关
CodePagesEncodingProvider.Instance
的更多信息。关于c# - 'UTF8'不是受支持的编码名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39065988/