Oswald Umeh
Oswald Umeh

Reputation: 104

Decoding Http Response Content from Russian (Cyrillic)

I send a request to an API and the server sends some portion of its response in Russian. I url decode the response using code page 1251 encoding but still don't get the result I want.

How can I convert the response back to plain english? What encoding do I use?

Upvotes: 1

Views: 1064

Answers (2)

retif
retif

Reputation: 1642

Not sure if I understood your intention correctly, but in case of HttpClient you can work with Windows-1251 (or another encoding) like this:

using (var httpClient = new HttpClient())
{
    var httpResponse = await httpClient.GetAsync("requestUri");
    var httpContent = await httpResponse.Content.ReadAsByteArrayAsync();
    string responseString = Encoding.GetEncoding(1251).GetString(httpContent, 0, httpContent.Length);

    // - check status code
    // (int)httpResponse.StatusCode
    // - and here's your response
    // responseString
}

If responseString still contains some gibberish, then I would assume that this server uses not Windows-1251 but some other encoding, so first you'll need to establish which one exactly.

P.S. For Encoding.GetEncoding(1251) to work you might need to install System.Text.Encoding.CodePages NuGet package and register encoding provider:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Upvotes: 1

Iskander Raimbaev
Iskander Raimbaev

Reputation: 1362

If you just need to convert Russian letters (Cyrillic) to Latin ones you can use Dictionary structure with Cyrillic-Latin relationship.

var map = new Dictionary<char, string>
{
    { 'Ж', "G" },
    { 'е', "e" },
    { 'ф', "f" },
    { 'Й', "Y" },
    ...
}

var result = string.Concat("Россия".Select(c => map[c]));

Upvotes: 2

Related Questions