Search code examples
c#httphttpclient

Http GET method returns file's content with wrong encoding


I have a simple GET request that returns .txt file's content. You can try it out using a browser or fiddler: http://134.17.24.10:8054/Documents/13f37b0c-4b04-42e1-a86b-3658a67770da/1.txt The encoding of this file is cp-1251. When I try to get it, I see smth like this: response text view

How can I modify HttpRequest in C# to get the text in the encoding that I want? Or just return only bytes in order to convert them manually. Is it possible with HttpClient?

Another attachment:

Request/Response data

Code snippet


Solution

  • If you can't change the charset sent by the server, yes, you can still just request the bytes and then choose which encoding to apply:

    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        
    using (var httpClient = new HttpClient())
    {   
        var response = await httpClient.GetByteArrayAsync(RequestUrl);
        var responseString = Encoding.GetEncoding("windows-1251").GetString(response, 0, response.Length - 1);
    
        Console.WriteLine(responseString);
    }
    

    References: How to change the encoding of the HttpClient response

    Encoding.GetEncoding can't work in UWP app

    The .NET Framework Class Library provides one static property, CodePagesEncodingProvider.Instance, that returns an EncodingProvider object that makes the full set of encodings available on the desktop .NET Framework Class Library available to .NET Core applications.

    https://learn.microsoft.com/en-us/dotnet/api/system.text.encodingprovider?redirectedfrom=MSDN&view=netcore-3.1