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();
                    }
                }
            }
5
  • using (HttpClient client = new HttpClient()) <- no! Read the docs "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." Commented Jan 4, 2022 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? Commented Jan 4, 2022 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<BR>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. Commented Jan 4, 2022 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. Commented Jan 4, 2022 at 12:55
  • Does this answer your question? Getting an UTF-8 response with httpclient in Windows Store apps Commented Jan 4, 2022 at 12:57

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

4 Comments

\u003c is not a HTML character, but unicode. Furthermore, he is actually receiving \\u003c
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).
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.