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
FtpWebRequest
always issues bothUSER
andPASS
FTP commands. So if you do not specify any password, theFtpWebRequest
sends aPASS
command 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:
PASS
command. Possibly it does not considerPASS
syntactically correct. Note that FileZilla sendsPASS<space>
, not justPASS
. You cannot makeFtpWebRequest
sendPASS<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, theFtpWebRequest
defaults to username "anonymous" and password "anonymous@". Try that.PASS
command. As I wrote above,FtpWebRequest
cannot do that. But you can try using a password"\r\n"
. This way theFtpWebRequest
sendsPASS \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.