UTF-8 is not a valid encoding name

60 Views Asked by At

I'm encountering an issue with an HTTP request in my .NET Framework Service. While the code works fine in all my clients, it throws an exception in one specific case:

enter image description here

The error messages are displayed in Portuguese, but in English, they are as follows:

"The character set provided in the ContentType is invalid." ... "UTF-8 is not a valid encoding name."

Here's the relevant code:

 requestMessage.Content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");

 TaskAwaiter<HttpResponseMessage> response;
 try
 {
     response = HTTPClient.SendAsync(requestMessage).GetAwaiter();
 }
 catch (Exception e)
 {
     throw new CDBAPIException($"Falha POST {url}", e);
 }
 try
 {
     var result = response.GetResult();
     if (result.StatusCode != System.Net.HttpStatusCode.OK)
     {
         throw new CDBAPIException($"Falha POST {result.StatusCode} - {result.ReasonPhrase}");
     }

     var content = response.GetResult().Content.ReadAsStringAsync().GetAwaiter();
     dynamic updates = JsonConvert.DeserializeObject(content.GetResult());

So, what do you suggest I can do to resolve this problem?

0

There are 0 best solutions below