由于某种原因,我需要用空/空白/无密码登录FTP服务器。
这应该是非常简单的。这里是代码:如何在C#中用空密码登录FTP?
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);
}
然而,这导致一个错误消息:
The remote server returned an error:(500) syntax error, command unrecognized.
网络跟踪日志:
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.]
于是,经过USER
命令有一个PASS
命令,但它被“语法错误”拒绝。这里发生了什么?如何使用空密码FtpWebRequest
类?
我使用FileZilla FTP客户端进行连接。下面是日志:
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
你想做什么?你从不设置FTP命令。你使用文件Uri而不是服务器Uri。也许你应该检查[类的文档中的示例](https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest%28v=vs.110%29.aspx)。无论如何,FTP中的匿名登录意味着你不指定凭证,或者你使用'anonymous/some @ email.address'作为用户名/密码。 –
当您只想下载时,请查看WebClient类。错误的空间较小。这确实留下了证书问题,请参阅以前的评论。 –
[启用日志记录](http://stackoverflow.com/q/9664650/850848)并将日志包含到您的问题中。 –