1. 在應用。NET 訪問FTP的Response來返回時,出現了如下錯誤。
2. 參考如下MSDN 和一些網絡資源,嘗試后解決了此問題.
> Ftp GetFileSize randomly throws FTP error 503 (Bad Sequence of Commands)
>FtpWebRequest re-sending USER and PASS commands half way through download loop
3. 問題是在此處應用此個FTP Request 之前曾經使用些地址創建了一個登錄Session, 所以在后面同一地址會話建立時會有這個異常報告。
The remote server returned an error: (503) Bad sequence of commands.
解決辦法就是將 request 對象的 KeepAlive 設為 False,其后基于同一IP創建的Session將不會拋出異常了。
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(sUnixURL));//"ftp://" + "10.2.159.69" + "/" + fileInf.Name));
reqFTP.Credentials = new NetworkCredential("dwei", "Admin98");
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false; // If keepalive is true will make the same IP request response lead to failed !
FileStream outputStream = new FileStream(FILE_NAME, FileMode.Create);
// Create the writer for data.
BinaryWriter w = new BinaryWriter(outputStream);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();