1

I'm having a problem with some characters when I use HTTPCLient, where it would have to come
comes \u003cBR\u003e, I would have to solve this I have to use .Replace("\u003c", "<") .Replace("\u003e", ">")?

Am I using the following code?

 using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add(header,
                    headerAuthenticationValue);
                
                using (HttpResponseMessage response = await client.GetAsync(url))
                {
                    using (HttpContent content = response.Content)
                    {
                        var tmpStr = await content.ReadAsStringAsync();
                    }
                }
            }
  • `using (HttpClient client = new HttpClient())` <- no! [Read the docs](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) _"HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors."_ – JHBonarius Jan 04 '22 at 11:31
  • Anyhow. What is your actual problem? That the data you are receiving is not in the format you want is to be? I don't think it's an issue with HttpClient, but with the data source. It seems to be unicode , maybe you're missing some json serializer? Or some other decoder? What's the source? – JHBonarius Jan 04 '22 at 11:34
  • Thanks for the using tip (HttpClient client = new HttpClient()). I'm updating my code .Net Frameworks 4.7.2 to .Net 6 and before the information came correctly. The text is: Peças e Acessórios Originais ARNO
    Redutor Completo And it's coming: Peças e Acessórios Originais ARNO\\u003cBR\\u003eRedutor Completo Another thing that has changed is that before I was on Visual Studio 2019 and Windows10 and now I'm on Ubuntu with VSCode, I started the migration on Ubuntu, but I don't think that's the problem.
    – Maykon Luiz Matos Araujo Jan 04 '22 at 12:00
  • How do you get the source? this looks like double encoding. Normally `\u003c` would resolve to a unicode character, but since the \ is escaped by an extra \, thus is not parsed correctly. – JHBonarius Jan 04 '22 at 12:55
  • Does this answer your question? [Getting an UTF-8 response with httpclient in Windows Store apps](https://stackoverflow.com/questions/22649473/getting-an-utf-8-response-with-httpclient-in-windows-store-apps) – JHBonarius Jan 04 '22 at 12:57

1 Answers1

1

You're receiving a string that contains escaped Unicode characters representing HTML characters. You can use WebUtility.HtmlDecode:

var str = WebUtility.HtmlDecode("\u003cBR\u003e"); // returns <BR>

or in your case:

var tmpStr = WebUtility.HtmlDecode(await content.ReadAsStringAsync());
Métoule
  • 13,062
  • 2
  • 56
  • 84
  • `\u003c` is not a HTML character, but unicode. Furthermore, he is actually receiving `\\u003c` – JHBonarius Jan 04 '22 at 12:55
  • It's the escaped unicode representation of the HTML character `<`, and using `WebUtility.HtmlDecode` correctly replaces it :) It's unclear if they receive `\u003c` (as stated in the question) or `\\u003c` (as stated in their comment). – Métoule Jan 04 '22 at 13:02
  • IMHO it's like using the back-end of a screwdriver to drive in a nail... HtmlDecode is to decode a string that has been html-encoded, not to fix other issues that can be solved a better way. – JHBonarius Jan 04 '22 at 13:21
  • I received \\u003c, the strange thing that when I use the code: var request = HttpWebRequest.Create(url); request.ContentType = "application/json"; request.Method = "GET"; request.Headers.Add(header, headerAuthenticationValue); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { var tmpStr = eader.ReadToEnd(); } } In .Net Frameworks 4.7.2 it works. – Maykon Luiz Matos Araujo Jan 04 '22 at 18:37