0

I'm writing a simple client for web API but can't get proper response. I receive json response using GetStringAsync method and have such strings:

title=\"\u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\">11 \u0444\u043e\u0442\u043e</a>"

"title" has cyrillic symbols. I tried:

var byteArray = await _cl.GetByteArrayAsync(url);
responseBody = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

with different encodings but result was the same.

I can get proper response using web browser or using request library in python. What's wrong with my code in c#?

Kanarsky
  • 162
  • 1
  • 10
  • I found a solution thanks to https://stackoverflow.com/a/32673917/2404234. But I still don't understand why I can't get json with already "un-escaped" strings? – Kanarsky Feb 10 '19 at 19:06
  • 3
    The `[HttpResponseMessage].Content.Headers` specify the `ContentEncoding` and/or `ContentType.CharSet`. You could try first using `HttpClient.GetAsync()`, which returns a `HttpResponseMessage`, then use the `ReadAsStringAsync()` method of the `HttpContent`, to see whether it gets it right, otherwise, use the `ReadAsByteArrayAsync()` method, get the Econding from the Headers, create a specific Encoding and decode the string (`[Encoding Type].GetString([Byte Array])`) – Jimi Feb 10 '19 at 19:34
  • 1
    Possible duplicate of [Simple way to un-escape escaped unpritable encoded string returned by mvc application](https://stackoverflow.com/questions/32672834/simple-way-to-un-escape-escaped-unpritable-encoded-string-returned-by-mvc-applic) – Stephen Kennedy Feb 10 '19 at 19:56

0 Answers0