2011-06-06 72 views
1

我嘗試從包含超過1000個文件的direcotry中從FTP讀取文件列表。C#FTP ListDirectoryDe​​tails問題

我不喜歡這樣寫道:

public static FtpWebRequest GetRequest(string uri) 
    { 
     FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 
     req.Credentials = new NetworkCredential("login", "password"); 
     req.KeepAlive = false; 
     req.UseBinary = false; 
     req.UsePassive = true; 
     return req; 
    } 

    public static bool CheckConnection() 
    { 
     FtpWebResponse respSize = null; 
     try 
     { 
      FtpWebRequest reqFTP = GetRequest(@"ftp://myftp.com"); 
      reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 
      respSize = (FtpWebResponse)reqFTP.GetResponse(); 
      respSize.Close(); 
      respSize = null; 
      reqFTP.GetResponse().Close(); 

      return true; 
     } 
     catch (Exception ex) 
     { 

     //... 
     } 
     finally 
     { 
      if (respSize != null) 
       respSize.Close(); 
     } 
     return false; 
    } 

我得到一個錯誤:

The remote server returned an error:

(451) Local error in processing.

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.SyncRequestCallback(Object obj)

at System.Net.FtpWebRequest.RequestCallback(Object obj)

at System.Net.CommandStream.Abort(Exception e)

at System.Net.CommandStream.CheckContinuePipeline()

at System.Net.FtpWebRequest.DataStreamClosed(CloseExState closeState)

at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)

at System.Net.FtpDataStream.Dispose(Boolean disposing)

at System.IO.Stream.Close()

at System.Net.FtpWebResponse.Close()

at CheckConnection()

有誰知道這是怎麼回事?

問候 馬辛

+0

它是否適用於較少數量的文件? – 2011-06-06 15:15:34

+0

是的,它適用於少量文件 – Marcin 2011-06-06 15:24:40

回答

1

從RhinoSoft(軟件的FTP Serv-U的的製造商):

"A 451 reply code may be sent in response to any command initiating a file transfer. It is a transient negative response, which means the error condition is a temporary one. It is usually sent in response to the server encountering an unexpected local error when processing data it is transferring or receiving. In this case, the client is encouraged to restart the FTP transaction and try again." [1]

所以,它可能與你的軟件和FTP服務器,不一定之間的通信問題您的軟件本身存在問題。

它可以增加Timeout財產FtpWebRequest的長度的傷害,但這不可能是基於我的研究的原因。

+0

你有什麼想法嗎?這個「臨時」問題每次都會發生。 也許管理員必須設置服務器上有一些標誌? – Marcin 2011-06-06 15:30:08

+0

我加倍超時,我認爲這不是問題... – Marcin 2011-06-06 15:34:13

+0

我測試了您的代碼與一個FTP服務器與2000個文件。不幸的是,錯誤是相當通用的,它是在FTP服務器上生成的,而不是在你的軟件中生成的。您最好的選擇是與服務器所有者合作。您對標準FTP客戶端有任何問題嗎? – 2011-06-06 15:38:33