3

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
jajanuj
  • 31
  • 1
  • 3
  • 5
    What are you trying to do? You never set the FTP command. You use a file Uri instead of a server Uri. Perhaps you should check the [samples in the class's documentation](https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest%28v=vs.110%29.aspx). In any case, anonymous login in FTP means either that you don't specify credentials, or that you use `anonymous/some@email.address` for username/password. – Panagiotis Kanavos Jun 10 '15 at 06:53
  • 1
    When you just want to download, look at the WebClient class. Less room for errors. That does leave the Credentials issue, see previous comment. – H H Jun 10 '15 at 07:03
  • [Enable logging](http://stackoverflow.com/q/9664650/850848) and include the log into your question. – Martin Prikryl Jun 10 '15 at 07:59
  • Anyways, seeing `Sending USER .. Okay .. Sending PASS .. Syntax error` is worrying. Did the FtpWebRequest really fail to send PASS with empty string? I've found a similar question on a different site, and [what they write there suggests a bug in FtpWebRequest class or in the FTP server](http://www.vbforums.com/showthread.php?653819-Error-with-FtpWebRequest-with-blank-password). Unfortunatelly, there's no reponse/solution there. I checked FTP specs and seems like the is mandatory after PASS. – quetzalcoatl Jun 10 '15 at 08:57
  • Can you login with any (even GUI) FTP client? If you can, can you post its log file? – Martin Prikryl Jun 10 '15 at 09:11
  • Did you try with just _not_ setting any credentials? Or with `NetworkCredential("VTIE", null);` ? – H H Jun 11 '15 at 06:32
  • Note that FileZilla uses command `PASS`, while `FtpWebRequest` uses `PASS`. The space likely makes the difference. Did you try [my suggestion with fake password](http://stackoverflow.com/a/30751928/850848)? – Martin Prikryl Jun 11 '15 at 06:39
  • If not setting any credentials get message - The remote server returned an error: (530) Not logged in; use NetworkCredential("VTIE", null) get message The call is ambiguous between the following methods or properties. – jajanuj Jun 11 '15 at 07:06

1 Answers1

0

The FtpWebRequest always issues both USER and PASS FTP commands. So if you do not specify any password, the FtpWebRequest sends a PASS 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:

  • Either your FTP server just requires a syntactically valid PASS command. Possibly it does not consider PASS syntactically correct. Note that FileZilla sends PASS<space>, not just PASS. You cannot make FtpWebRequest send PASS<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, the FtpWebRequest defaults to username "anonymous" and password "anonymous@". Try that.
  • Or your password is really an empty string (and it really gets validated). And the FTP server requires the space (argument separator) in PASS command. As I wrote above, FtpWebRequest cannot do that. But you can try using a password "\r\n". This way the FtpWebRequest sends PASS \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.
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I use the fake password like this " request.Credentials = new NetworkCredential("VTIE", "dd");", and get message - The remote server returned an error: (530) Not logged in.. – jajanuj Jun 11 '15 at 07:01