I am trying to download a file via FTP, using the WebClient class. The problem is, one of the directory comprising the file path on the FTP server contains whitespaces.
And, even though my URI string has whitespaces, they are all converted to "%20" automatically when using the DownloadData method, therefore the directory isn't found and the download fails.
My question is: Is there a way to force the WebClient not to convert my whitespaces into %20 ?
Here is my code:
using (WebClient request = new WebClient())
{
request.Credentials = new NetworkCredential("login", "password");
string url = ("ftp://XXX.XXX.XX.XXX:21/root/folder1/folder with spaces/pic1.jpg");
byte[] fileData = request.DownloadData(url);
using (FileStream file = File.Create(localpath))
{
file.Write(fileData, 0, fileData.Length);
file.Close();
}
}
Using Wireshark, this is my network log:
Request: USER login
Response: 331 Please specify the password
Request: PASS password
Response: 230 Login successful
Request: OPTS utf8 on
Response: 200 Always in UTF8 mode.
Request: PWD
Response: 257 "/home/login"
Request: CWD home/login/root/folder1/folder%20with%20spaces/
Response: 550 Failed to change directory
Log for trying to download folder1 (note that CWD did not fail, I just got an error 'cause it expected a file and not a folder):
Response: 230 Login successful
Request: OPTS utf8 on
Response: 200 Always in UTF8 mode.
Request: PWD
Response: 257 "/home/login"
Request: CWD home/login/root/
Response: 250 Directory successfully changed
Request: RETR folder1
Response: 550 Failed to open file.
Log for downloading pic1.jpg with FileZilla (note that the difference is that there are no %20 in the file path):
Response: 230 Login successful
Request: OPTS UTF8 on
Response: 200 Always in UTF8 mode.
Request: CWD home/login/root/folder1/folder with spaces/
Response: 250 Directory successfully changed
Request: RETR pic1.jpg
Response: 150 Opening BINARY mode data connection for pic1.jpg
Response: 226 Transfer complete.