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