FtpWebRequest无法在测试环境中连接

时间:2017-01-24 13:05:38

标签: c# ftp ftpwebrequest

我试图连接到FTP服务器并获得超时错误,在我的结束它可以工作,但在测试环境中我得到超时错误,我已禁用防火墙,用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()

Pasive Mode

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()

1 个答案:

答案 0 :(得分:0)

尝试设置request.UseBinary = true;