Downloading a file with RTL characters in the name, with C# WebRequest

354 views Asked by At

I am trying to download a file which has hebrew characters in the name

https://example.com/path/‏צילום מסך 2014‏.04‏.16 ב‏.16.44.30.png

When I try to download with the browser, the filename is correctly encoded and the server returns the file.

If I am downloading with C# code from the server, the filename is not encoded properly thus the server returns error 403.

If I encode the filename using HttpUtility.UrlEncode() and pass it to the WebRequest class, it is encoded properly but it has the same result (error 403).

I inspected the web calls with Fiddler and the encoded filename is different than what browser is encoding. If I get the filename and decode it, the filename is different (see below)

https://example.com/path/צילום מסך 2014.04.16 ב.16.44.30.png

I suspect that the problem is that the filename is partially encoded with Right-To-Left characters and the WebRequest class isn't equipped with the methods to handle it. please see below the code used to download all of the files.


private byte[] GetFile(string url)
{
    byte[] result;
    byte[] buffer = new byte[4096];
    WebRequest request = WebRequest.CreateHttp(url);

    using (var remoteStream = request.GetResponse().GetResponseStream())
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            int count = 0;

            do
            {
                count = remoteStream.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, count);
            } 
            while (count != 0);

            result = memoryStream.ToArray();
        }
    }

    return result;
}

0

There are 0 answers