我有以下代码:
var listDirectoryFtpRequest = (FtpWebRequest) WebRequest.Create(directoryUri);
listDirectoryFtpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
listDirectoryFtpRequest.Credentials = /* my credentials */;
listDirectoryFtpRequest.KeepAlive = false;
listDirectoryFtpRequest.UsePassive = true;
listDirectoryFtpRequest.UseBinary = true;
using (var response = listDirectoryFtpRequest.GetResponse())
{
return true;
}
存储在 中的目录directoryUri
确实存在。
我的 ftp 服务器有以下日志
(022102) 30.05.2012 08:32:18 - **USER** (213.235.245.89)> 230 Logged on
(022102) 30.05.2012 08:32:19 - **USER** (213.235.245.89)> OPTS utf8 on
(022102) 30.05.2012 08:32:19 - **USER** (213.235.245.89)> 200 UTF8 mode enabled
(022102) 30.05.2012 08:32:20 - **USER** (213.235.245.89)> PWD
(022102) 30.05.2012 08:32:20 - **USER** (213.235.245.89)> 257 "/" is current directory.
(022102) 30.05.2012 08:32:21 - **USER** (213.235.245.89)> CWD /21792543/2/
(022102) 30.05.2012 08:32:21 - **USER** (213.235.245.89)> 250 CWD successful. "/21792543/2" is current directory.
(022102) 30.05.2012 08:32:22 - **USER** (213.235.245.89)> TYPE I
(022102) 30.05.2012 08:32:22 - **USER** (213.235.245.89)> 200 Type set to I
(022102) 30.05.2012 08:32:23 - **USER** (213.235.245.89)> PASV
(022102) 30.05.2012 08:32:23 - **USER** (213.235.245.89)> 227 Entering Passive Mode (10,40,31,10,229,112)
(022102) 30.05.2012 08:32:27 - **USER** (213.235.245.89)> NLST
(022102) 30.05.2012 08:32:27 - **USER** (213.235.245.89)> 150 Connection accepted
(022102) 30.05.2012 08:32:27 - **USER** (213.235.245.89)> 226 Transfer OK
(022102) 30.05.2012 08:32:29 - **USER** (213.235.245.89)> disconnected.
不过我得到以下异常:
System.Net.WebException: The underlying connection was closed: The server committed a protocol violation.
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.InvokeRequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetResponse()
at **MYCLASS**.**MYMETHOD**
为什么?...我该如何解决这个问题?
PS .:我在这里发现了另一个类似症状的问题。但是我真的必须去KeepAlive = true
-hack 吗?