0

I download the file by POST request. The encoded file name comes in the response headers. How can I decode it?

I need "ЕГРН__26-29-040306-520__XZP.XML" (in Fiddler correct), but in my program i get "ÐÐРÐ__26-29-040306-520__XZP.XML".

Response Body in right encoding.

This is screenshot - https://i.stack.imgur.com/CnL6M.png

ASP.NET, MVC, Framework 4.6

1 Answers1

0

.Net does support Unicode fully and all strings are internally stored as UTF-16. It's possible your IDE is set to an encoding that cant display the string, but it's still stored correctly in memory. The problem is that the HTTP specification only supports ASCII in the headers, no other encoding is guaranteed to work, event though it often does.

Link to specification: https://www.rfc-editor.org/rfc/rfc2183 which states that filenames in Content-Disposition only supports ASCII.

You can read more about it here: What character encoding should I use for a HTTP header?

If possible you should convert the filenames to base64 on the client before uploading, but if the uploads are from a browser then that's not really possible.

One thing you could try is this:

var textBytes = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(response.Content.Headers.ContentDisposition.FileName);
var converted = System.Text.Encoding.UTF8.GetString(textBytes);
Community
  • 1
  • 1
JonC
  • 978
  • 2
  • 7
  • 28