How to login FTP with blank password in C #?
For some reason, I need to log into the FTP server with a blank / blank / no password.
It should be pretty simple. Here is the 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 trace 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 USER
there is a command after the command PASS
, but it is rejected with "Syntax Error". What's going on here? How can I use a class FtpWebRequest
with an empty password?
I am using FileZilla FTP client to connect. Here is the 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
source to share
FtpWebRequest
always issues commands USER
and PASS
FTP. Therefore, if you do not provide a password, it FtpWebRequest
sends the command PASS
with no argument. Your server doesn't like this. Although you argue that you don't need to use a password to login.
There are several options:
- Either your FTP server just requires a syntactically correct command
PASS
. Perhaps he does not consider it syntactically correctPASS
. Note that FileZilla submitsPASS<space>
, not onlyPASS
. You can not directly sendFtpWebRequest
sendPASS<space>
. So maybe the FTP server doesn't care what password you use. Try using a random string for your password. If you do not provide a username,FtpWebRequest
the default username is "anonymous" and the password is "anonymous @". Try it. - Or your password is really an empty string (and it is actually being verified). And the FTP server needs space (argument separator) in the command
PASS
. As I wrote above,FtpWebRequest
it cannot do this. But you can try using a password"\r\n"
. Thus, itFtpWebRequest
sendsPASS \r\n\r\n\
. That isPASS<space>
, followed by an empty string. Hopefully the FTP server is ignoring the blank line. I have tested with FileZilla FTP server and it ignores empty line. But this may be server specific.
source to share