Reputation: 4259
I am using this code to connect my server and downloading the files. It is working fine.
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)
{
}
}
}
But client needs it to be secure FTP. So I tried by setting
request.EnableSsl = true;
as specified in Differences between SFTP and "FTP over SSH"
And it throws:
The remote server returned an error: (500) Syntax error, command unrecognized.
Upvotes: 1
Views: 7138
Reputation: 202272
You are obviously required to use the SFTP protocol.
The FtpWebRequest
class does not support the SFTP protocol. There's no support at all for SFTP in standard .NET libraries. See SFTP Libraries for .NET.
You current code is trying to connect with the FTP over TLS protocol to the FTP server. That is an another way of securing "file transfer" session. But this particular FTP server does not support it (hence the "500 Syntax error, command unrecognized" error).
So you have to rewrite your code to use the SFTP protocol.
That's unfortunately a completely different code.
Upvotes: 1