C# 在FTP会话中启用安全性后,出现500语法错误,无法识别命令
我正在使用此代码连接我的服务器并下载文件。它工作得很好C# 在FTP会话中启用安全性后,出现500语法错误,无法识别命令,c#,ftp,sftp,ftpwebrequest,C#,Ftp,Sftp,Ftpwebrequest,我正在使用此代码连接我的服务器并下载文件。它工作得很好 public void downloadFile(object args) { writeStream = null; response = null; reader = null; int dataLength = 0; try { Array argArray = new object[3]; argArray = (Array)args;
public void downloadFile(object args)
{
writeStream = null;
response = null;
reader = null;
int dataLength = 0;
try
{
Array argArray = new object[3];
argArray = (Array)args;
ProgressBar progressBar1 = (ProgressBar)argArray.GetValue(0);
Label lbProgress = (Label)argArray.GetValue(1);
FTPInfo ftpInfo = (FTPInfo)argArray.GetValue(2);
string ipAddress = ftpInfo.IpAddress;
string path = ftpInfo.Path;
string fileName = ftpInfo.FileName;
path = Regex.Replace(path, "_.", "_e");
fileName = Regex.Replace(fileName, "_.", "_e");
string uri = null;
if (path.Equals(""))
{
uri = ipAddress + fileName;
}
else
{
uri = ipAddress + path + "/" + fileName;
}
string[] temp = ipAddress.Split('/');
string ip = "mchmultimedia.com";
string userName = ftpInfo.UserName;
string password = ftpInfo.Password;
downloadedData = new byte[0];
ftp = new FTPClass(path);
ftp.FtpServer = ip;
ftp.FtpUsername = userName;
ftp.FtpPassword = password;
ftp.FtpLogin();
dataLength = (int)ftp.GetFileSize(fileName);
Logger.LogDebugMessage("DataLength :" + dataLength.ToString());
ftp.CloseConnection();
FtpWebRequest request = FtpWebRequest.Create(uri) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(userName, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
//Set up progress bar
UpdateProgressBarValue(progressBar1, 0);
SetProgressBarMaxValue(progressBar1, dataLength);
response = request.GetResponse() as FtpWebResponse;
reader = response.GetResponseStream();
if (!Directory.Exists(GlobalClass.ZIPPED_FOLDER))
Directory.CreateDirectory(GlobalClass.ZIPPED_FOLDER);
writeStream = new FileStream(GlobalClass.ZIPPED_FOLDER + "\\" + fileName, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = 0;
Logger.LogDebugMessage("Before while :" + dataLength.ToString());
while (true)
{
Application.DoEvents();
bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
try
{
try
{
reader.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("reader close if bytesRead ==00", ex);
}
try
{
response.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("response close if bytesRead ==00", ex);
}
try
{
writeStream.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("writeStream close if bytesRead ==00", ex);
}
}
catch (Exception ex)
{
}
UpdateProgressBarValue(progressBar1, progressBar1.Maximum);
Application.DoEvents();
break;
}
else
{
writeStream.Write(buffer, 0, bytesRead);
if (progressBar1.Value + bytesRead <= progressBar1.Maximum)
{
totalBytesRead = progressBar1.Value + bytesRead;
int percentage = (int)((double)totalBytesRead / dataLength * 100);
UpdateProgressBarValue(progressBar1, totalBytesRead);
SetText(lbProgress, "File download " + percentage.ToString() + " % completed");
if (percentage == 100)
{
if (totalBytesRead == dataLength)
{
try
{
try
{
reader.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("reader close if percentage==100", ex);
}
try
{
response.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("response close if percentage==100", ex);
}
try
{
writeStream.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("writeStream close if percentage==100", ex);
}
}
catch (Exception ex)
{
}
SetText(lbProgress, "File download successfully completed,Please wait...");
unzipFile(fileName);
}
}
RefreshProgressBar(progressBar1);
Application.DoEvents();
}
}
}
}
catch (System.Threading.ThreadAbortException ex)
{
if (thread != null)
{
thread.Abort();
}
Logger.LogErrorMessage("ThreadAbortException", ex);
}
catch (Exception ex)
{
Logger.LogErrorMessage("Exception there was an error connecting", ex);
Logger.ReportBug("There was an error connecting to the FTP Server.", ex);
}
finally
{
try
{
try
{
reader.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("read finally", ex);
}
try
{
response.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("resonse finally", ex);
}
try
{
writeStream.Close();
}
catch (Exception ex)
{
Logger.LogErrorMessage("writeStream finally", ex);
}
}
catch (Exception ex)
{
}
}
}
如
它抛出:
远程服务器返回错误:(500)语法错误,无法识别命令
显然,您需要使用SFTP协议 该类不支持SFTP协议。在标准.NET库中根本不支持SFTP。看 您当前的代码正在尝试使用FTP over TLS协议连接到FTP服务器。这是另一种确保“文件传输”会话安全的方法。但是这个特定的FTP服务器不支持它(因此出现“500语法错误,命令无法识别”错误) 因此,您必须重写代码以使用SFTP协议
不幸的是,这是一个完全不同的代码。那么客户端是否要求您使用安全FTP(根据您使用的文本和核心)或SFTP(您使用的标记)?在阅读时,你真的理解了其中的区别吗?500错误表示服务器不理解
AUTH TLS
命令,因此不支持通过TLS的FTP。也许你应该使用SFTP?ftpInfo.IpAddress
中是否指定了端口?我有点困惑。我使用了SSH.NET Nuget包,它运行得很好,但我刚刚遇到一个链接,上面说可以通过启用SSl来启用安全性,所以我已经尝试过了。嘿,我想问一下,从普通ftp(内置.NET)下载文件所用的时间是否有任何影响而且使用SSH.NET或任何其他第三方SFTP服务SSFTP可能会更慢。1) 由于加密,如果传输的任何一方都限制了CPU功率。2) SFTP(和底层SSH)协议是面向数据包的。如果它们的实现不理想(同样是在任何一侧),那么与服务器之间的连接的容量可能不会得到最大利用。这两者都不是FTP协议的问题。FTP协议在处理大量小文件时,效率不如SFTP协议。
request.EnableSsl = true;