我正在使用此代码连接我的服务器并下载文件。它工作正常。
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)
{
}
}
}
但客户需要它是安全的FTP。所以我试着设置
request.EnableSsl = true;
在Differences between SFTP and "FTP over SSH"
中指定它抛出:
远程服务器返回错误:(500)语法错误,命令无法识别。
答案 0 :(得分:1)
您显然需要使用SFTP协议。
FtpWebRequest
类不支持SFTP协议。在标准.NET库中,SFTP根本不支持。请参阅SFTP Libraries for .NET。
您当前的代码正在尝试使用FTP over TLS协议连接到FTP服务器。这是保护“文件传输”会话的另一种方法。但是这个特定的FTP服务器不支持它(因此“500语法错误,命令无法识别”错误)。
因此,您必须重写代码才能使用SFTP协议。
不幸的是,这是一个完全不同的代码。