For some reason I need to login FTP server with empty/blank/no password.
That should be pretty straightforward. Here is code:
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/folder/file.csv");
request.Credentials = new NetworkCredential("VTIE", "");
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
StreamWriter writer = new StreamWriter(@"D:\\text.txt");
writer.Write(reader.ReadToEnd().ToString());
writer.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
However, this results in an error message:
The remote server returned an error:(500) syntax error, command unrecognized.
Network Tracing Log:
System.Net Information: 0 : [4864] FtpWebRequest#1013293::.ctor(ftp://xxx.xxx.xxx.xxx/folder/file.csv)
System.Net Information: 0 : [4864] FtpWebRequest#1013293::GetResponse(Method=RETR.)
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Created connection from YYY.YYY.YYY.YYY:57358 to xxx.xxx.xxx.xxx:21.
System.Net Information: 0 : [4864] Associating FtpWebRequest#1013293 with FtpControlStream#45598209
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Received response [220 VT-E1 FTP server ready.]
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Sending command [USER UserName]
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Received response [331 User name okay, need password.]
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Sending command [PASS]
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Received response [500 Syntax error, command unrecognized.]
So, after USER command there's a PASS command, but it is rejected with "Syntax Error". What is going on here? How can I use FtpWebRequest class with empty password?
I use FileZilla FTP client to connect. Here is log:
Status: Connecting to xxx.xxx.xxx.xxx:21...
Status: Connection established, waiting for welcome message...
Response: 220 VT-E1 FTP server ready.
Command: USER VTIE
Response: 331 User name okay, need password.
Command: PASS
Response: 230 User logged in, proceed.
Command: SYST
Response: 202 Command not implemented.
Command: FEAT
Response: 500 Syntax error, command unrecognized.
Status: Server does not support non-ASCII characters.
Status: Connected
Status: Retrieving directory listing...
Command: PWD
Response: 257 "/" is current directory.
Command: TYPE I
Response: 200 TYPE command successful.
Command: PASV
Response: 227 Entering Passive Mode (xxx,xxx,xxx,xxx,18,46).
Command: LIST
Response: 150 Opening data connection for (LIST) (xxx.xxx.xxx.xxx,4654).
Response: 226 Transfer complete.
Status: Directory listing of "/" successful
The
FtpWebRequestalways issues bothUSERandPASSFTP commands. So if you do not specify any password, theFtpWebRequestsends aPASScommand without an argument. Your server does not like that. Although you claim that you do not need to use a password to login.There several options:
PASScommand. Possibly it does not considerPASSsyntactically correct. Note that FileZilla sendsPASS<space>, not justPASS. You cannot makeFtpWebRequestsendPASS<space>directly. So maybe the FTP server does not care what password your use. Try to use a random string for password. When you do not specify any username, theFtpWebRequestdefaults to username "anonymous" and password "anonymous@". Try that.PASScommand. As I wrote above,FtpWebRequestcannot do that. But you can try using a password"\r\n". This way theFtpWebRequestsendsPASS \r\n\r\n\. I.e.PASS<space>followed by an empty line. Hopefully the FTP server ignores the empty line. I've tested with with FileZilla FTP server and it does ignore the empty line. But this can be server-specific.