为什么我的ListDirectory ftp-request会抛出异常

时间:2012-05-30 12:45:14

标签: ftpwebrequest

我有以下代码:

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-server的日志

(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:我在SO上发现了another question,症状相似。但我真的要去KeepAlive = true - 黑客吗?

1 个答案:

答案 0 :(得分:0)

哦亲爱的... .Net实现直到3.5才修复 - 可能是4.0 ... 无论如何:我使用System.Net.FtpClient来修复我的上传/下载/请求...