出于某种原因,我需要使用空/空/无密码登录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);
}
但是,这会导致错误消息:
远程服务器返回错误:(500)语法错误,命令无法识别。
网络跟踪日志:
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
答案 0 :(得分:0)
FtpWebRequest
始终发出USER
和PASS
FTP命令。因此,如果您未指定任何密码,FtpWebRequest
将发送PASS
命令而不带参数。你的服务器不喜欢这样。虽然您声称不需要使用密码登录。
有几种选择:
PASS
命令。可能它不认为PASS
在语法上是正确的。请注意,FileZilla会发送PASS<space>
,而不仅仅是PASS
。您无法直接FtpWebRequest
发送PASS<space>
。所以也许FTP服务器不关心你使用什么密码。尝试使用随机字符串作为密码。如果您未指定任何用户名,则FtpWebRequest
默认使用用户名“anonymous”,密码为“anonymous @”。试试吧。PASS
命令中的空格(参数分隔符)。正如我上面所写,FtpWebRequest
不能这样做。但您可以尝试使用密码"\r\n"
。这样FtpWebRequest
发送PASS \r\n\r\n\
。即PASS<space>
后跟一个空行。希望FTP服务器忽略空行。我已经使用FileZilla FTP服务器进行了测试,它确实忽略了空行。但这可能是特定于服务器的。