'UTF8' is not a supported encoding name
Asked Answered
E

3

22

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;
}
Extradite answered 21/8, 2016 at 15:44 Comment(3)
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: github.com/spotify/web-api/issues/291Bast
I solved a similar problem by specifying the proxy.Denounce
You could just read it in advance and then fix the error before giving it to the deserializer...Acoustics
P
30

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.

Pistachio answered 21/8, 2016 at 15:59 Comment(6)
No, I'm using .NET 4.5 @SephrExtradite
So that's not the case... anyone else?Extradite
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.'Arri
This solved an encoding error I was receiving when using an external .dll that was throwing an encoding errorQuasimodo
Fixed the problem here.Grondin
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.Delinda
T
20

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!!!

Trixy answered 17/1, 2017 at 14:24 Comment(3)
Might be best to actually check for that "UTF8" string in the bytes you receive to ensure it's meant to be UTF-8, and only fix it if you need to fix it.Acoustics
There is also Encoding.WebName, that might be even more exact in this caseOrthorhombic
You made my day :)Sino
R
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.

Radie answered 30/9, 2019 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.