Loading file with RTL characters in name, with C # WebRequest

I am trying to load a file with Hebrew characters in the name

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

      

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

If I download C # code from the server, the filename is not encoded correctly, so the server returns a 403 error.

If I encode the filename with HttpUtility.UrlEncode()

and pass it to the class WebRequest

, it is encoded correctly but has the same result (403 error).

I have checked web calls with Fiddler and the encoded filename is different from what the browser encodes. 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 the problem is that the file name is partially encoded with Right-To-Left characters and the WebRequest class is not equipped with methods to handle it. see below code used to download all 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;
}

      


+3


source to share





All Articles