1
我正在運行此代碼以通過ftp將本地文件傳輸到另一臺服務器。問題是我正在定期收到錯誤遠程服務器返回錯誤:(421)服務不可用,關閉控制連接。代碼是否存在問題,是更快速傳輸文件的更好方式。所有的文件都需要被轉移,所以我想了一個while循環,並捕獲錯誤,直到文件夾中的所有文件都被轉移。這是我得到的週期性錯誤:遠程服務器返回錯誤:(421)服務不可用,關閉控制連接
foreach (FileInfo file in files)
{
try
{
// Get the object used to communicate with the server.
FtpWebRequest request =
(FtpWebRequest)
WebRequest.Create(String.Format("{0}{1}/{2}", Host, destinationPath,
file.Name));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.KeepAlive = false;
/* 20 mins timeout */
request.Timeout = 1200000;
request.ReadWriteTimeout = 1200000;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(Username, Password);
// Copy the contents of the file to the request stream.
byte[] fileContents = File.ReadAllBytes(file.FullName);
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
//using (FtpWebResponse response = (FtpWebResponse) request.GetResponse())
//{
//}
if (deleteSourcePath)
{
File.Delete(file.FullName);
}
}
catch (Exception ex)
{
// Log.Warn("Error Moving Images", ex.Message);
}
}
看起來,人們可以通過排除異常的catch語句來判斷代碼片段的質量。 –