17

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' 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.

The ContentType is application/json; charset=UTF8

Any ideas?

Here's my request code:

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;
}
Falko
  • 15,326
  • 12
  • 50
  • 91
Fredrik
  • 213
  • 1
  • 3
  • 9
  • I think this is just a bug in the Spotify Web API, sending charset=UTF8 instead of charset=utf-8 for this endpoint. It is filed here: https://github.com/spotify/web-api/issues/291 – jooon Aug 23 '16 at 12:06
  • I solved a similar problem by [specifying the proxy](https://stackoverflow.com/q/16526689/86967). – Brent Bradburn Oct 20 '17 at 16:35
  • You could just read it in advance and then fix the error before giving it to the deserializer... – Nyerguds Feb 03 '18 at 18:06

3 Answers3

23

Are you using .net core?

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);

More info on CodePagesEncodingProvider.Instance can be found here.

Simon Farshid
  • 2,429
  • 17
  • 29
  • No, I'm using .NET 4.5 @Sephr – Fredrik Aug 21 '16 at 16:04
  • So that's not the case... anyone else? – Fredrik Aug 22 '16 at 11:42
  • This fixes all encoding errors except of : System.ArgumentException: ''iso-2022-cn' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.' – Ronen Festinger Apr 09 '18 at 10:36
  • This solved an encoding error I was receiving when using an external .dll that was throwing an encoding error – hanzolo Feb 08 '19 at 21:30
  • Fixed the problem here. – Nime Cloud Apr 19 '20 at 21:05
  • after adding this line , httpResponseMessage.Content.ReadAsStringAsync() also works automatically for encoding like windows-1252 which was throwing exception before that and there is no need to add byte array logic extraction and then converting directly using specific encoding. – Iman Jan 28 '21 at 07:26
17

The problem should be a validation of response header Content-Type ,that occur when you call ReadAsStringAsync(), if you call ReadAsByteArrayAsync() instead and parse to string

(System.Text.Encoding.UTF8.GetString())

that will gonna work!!!

ρяσѕρєя K
  • 127,886
  • 50
  • 184
  • 206
Lucas Micheleto
  • 171
  • 1
  • 3
1

I had a same problem while I was trying to get an answer from my API which is built in PHP using C# service. I could fix the issue by changing "charset=UTF8" to "charset=\"UTF-8\"" on the PHP side(the api that sends result to the C# service). Hope this helps someone.

J_Mok
  • 45
  • 4