2

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

  • Please click Raw and show the request and response in your question. Also show what C# code you currently have. – CodeCaster Aug 12 '20 at 22:51
  • So what's your question? How to change the encoding of a response that you generate from ASP.NET, or how to change the encoding of a string you download using HttpClient? – CodeCaster Aug 12 '20 at 23:09
  • How to change the encoding of a response. (To be honest, I'd prefer whatever solution just to get the initial view of the text) – Roman Larrsen Aug 12 '20 at 23:15
  • Btw, sorry, but I can't upload the source code, because it is a part of a private project. Moreover, all the actions with Http are extracted to the separate file and it's hard to display you the whole picture. – Roman Larrsen Aug 12 '20 at 23:17
  • Long story short, this code snippet can describe my problem (last update) – Roman Larrsen Aug 12 '20 at 23:20
  • If you can't show us a [mre], we can't help you. We need to know where in the process the data is written and how the headers are determined. For all I know, all you need to change is the content-type header to `Content-Type: text/plain; charset=cp-1251`... – CodeCaster Aug 12 '20 at 23:20
  • @CodeCaster Do you know how to do it using HttpClient? – Roman Larrsen Aug 12 '20 at 23:22
  • You download the bytes and interpret them in the encoding you want. – CodeCaster Aug 12 '20 at 23:23
  • Also, I think you must [configure your console to support Unicode](https://stackoverflow.com/questions/5055659/c-sharp-unicode-string-output) for displaying Cyrillic characters. – CodeCaster Aug 12 '20 at 23:40
  • The HTTP response headers indicate the file is `text/plain` without specifying a character set. In that case, the browser (and Fiddler) will assume it's UTF8. If you want the response body to be interpreted as cp-1251, the server needs to set an appropriate [charset](https://www.w3.org/International/articles/http-charset/index) in the HTTP headers. – John Wu Aug 13 '20 at 00:21

1 Answers1

1

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

Kimberly
  • 2,657
  • 2
  • 16
  • 24