FtpWebRequest 测试环境连接失败

Posted

技术标签:

【中文标题】FtpWebRequest 测试环境连接失败【英文标题】:FtpWebRequest Fail to connect in Testing environment 【发布时间】:2017-01-24 13:05:38 【问题描述】:

我正在尝试连接到 FTP 服务器并收到超时错误,在我这端它可以工作,但在测试环境中我收到超时错误,我已禁用防火墙,将 url 替换为 IP,但它仍然没有在那里工作

我正在使用此代码:

var remoteFTPPath = ftpServer.StartsWith("ftp://") ? ftpServer : string.Format("ftp://0", ftpServer);
            var request = (FtpWebRequest)WebRequest.Create(remoteFTPPath);
            var networkCredentials = new NetworkCredential(userName, password);
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.Credentials = networkCredentials;
            response = request.GetResponse();
            
            finally
            
                 response.Close();
                 response.Dispose();

            

我在主动和被动模式下都遇到错误

主动模式:

System.Net Information: 0 : [4784] FtpControlStream#1013293 - Sending command [PORT -----]
System.Net Information: 0 : [4784] FtpControlStream#1013293 - Received response [200 PORT command successful.]
System.Net Information: 0 : [4784] FtpControlStream#1013293 - Sending command [NLST]
System.Net Information: 0 : [4784] FtpControlStream#1013293 - Received response [150 Opening BINARY mode data connection.]
System.Net Information: 0 : [10856] ServicePoint#50726992 - Closed as idle.
System.Net Information: 0 : [4784] FtpWebRequest#54814357::(Releasing FTP connection#1013293.)
System.Net Error: 0 : [4784] Exception in FtpWebRequest#54814357::GetResponse - The operation has timed out.
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
   at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.GetResponse()

被动模式

System.Net Information: 0 : [4784] FtpControlStream#1013293 - Sending command [TYPE I]
System.Net Information: 0 : [4784] FtpControlStream#1013293 - Received response [200 Type set to I.]
System.Net Information: 0 : [4784] FtpControlStream#1013293 - Sending command [PASV]
System.Net Information: 0 : [4784] ServicePoint#50726992 - Closed as idle. 
System.Net Information: 0 : [4784] FtpWebRequest#54814357::(Releasing FTP connection#1013293.)
System.Net Error: 0 : [4784] Exception in FtpWebRequest#54814357::GetResponse - The operation has timed out.
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
   at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.GetResponse()

【问题讨论】:

登录文本机器并尝试使用常规客户端连接到 FTP 服务器。似乎服务器在初始连接后没有响应任何命令。 感谢您的回答,但适用于 Filezilla 【参考方案1】:

尝试设置request.UseBinary = true;

【讨论】:

以上是关于FtpWebRequest 测试环境连接失败的主要内容,如果未能解决你的问题,请参考以下文章

使用 FtpWebRequest 附加到大型机上的文件时出现问题

FtpWebRequest打开随机端口而不是21

FtpWebRequest/FtpWebResponse 上的 OPTS 命令

使用 C# 到 FTP 文件到大型机,包括数据集 - 将 FTP 脚本翻译成 FtpWebRequest 代码

FtpWebRequest 下载文件

FTP上传失败报错227 Entering Passive Model (222,111,8,111,10,40)