If you download multiple files, sometimes inexplicably 500 server errors, it is possible that the KeepAlive property is not set to cause.
An unhandled exception occurred in the application: 2015/1/6 11:40:56
Exception type: WebException
Exception message: The remote server returned an error: (500) syntax error, command not recognized.
Reference: http://www.cnblogs.com/webabcd/archive/2007/01/21/626242.html
KeepAlive-Specifies whether the connection should be closed or closed after the request is complete, by default true
/// <summary>
/// FTP download file (with progress bar)
/// </summary>
/// <param name="filename"></param>
Public void DownloadFile(string filename)
{
Float percent = 0;
String filePathName = string.Empty;
String url = string.Empty;
filePathName = Path.Combine(Application.StartupPath, filename);
String dirPath = GetDirPath(filePathName);
If (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
//=>Replace the path in the file directory as the network path
Filename = filename.Replace("\\", "/");
Url = "ftp://" + clientUpdateInfo.UpdateFTPIP + "/" + clientUpdateInfo.UpdatePath + "/" + filename;
Var reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFtp.Method = WebRequestMethods.Ftp.DownloadFile;
reqFtp.UseBinary = true;
reqFtp.KeepAlive = false;//Be sure to set this property, otherwise an exception will occur when downloading multiple files at once.
reqFtp.Credentials = new NetworkCredential(clientUpdateInfo.FtpUserName, clientUpdateInfo.FtpUserPwd);
Var response = (FtpWebResponse)reqFtp.GetResponse();
Long totalBytes = response.ContentLength;
If (prog != null)
{
this.BeginInvoke(new MethodInvoker(delegate()
{
prog.Maximum = (int)totalBytes;
}));
}
Stream st = response.GetResponseStream();
Var so = new FileStream(filePathName, FileMode.Create);
Long totalDownloadedByte = 0;
Byte[] by = new byte[1024];
Int osize = st.Read(by, 0, (int)by.Length);
While (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
so.Write(by, 0, osize);
If (prog != null)
{
this.BeginInvoke(new MethodInvoker(delegate()
{
prog.Value = (int)totalDownloadedByte;
}));
}
Osize = st.Read(by, 0, (int)by.Length);
Percent = (float)totalDownloadedByte * 1.0f / (float)totalBytes * 100;
Application.DoEvents();
this.BeginInvoke(new MethodInvoker(delegate()
{
lbDownInfo.Text = "Downloading" + filename + ", download progress is: " + Math.Round(percent, 2) + "%";
lbDownInfo.Refresh();
}));
Application.DoEvents();
}
so.Close();
st.Close();
response.Close();
}
Private void FtpDownload(string filename)
{
String filePathName = string.Empty;
String url = string.Empty;
filePathName = Path.Combine(Application.StartupPath, filename);
String dirPath = GetDirPath(filePathName);
If (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
//=>Replace the path in the file directory as the network path
Filename = filename.Replace("\\", "/");
Url = "ftp://" + clientUpdateInfo.UpdateFTPIP + "/" + clientUpdateInfo.UpdatePath + "/" + filename;
FtpWebRequest reqFTP;
this.BeginInvoke(new MethodInvoker(delegate()
{
this.lbDownInfo.Text = "Start downloading...";
}));
FileStream outputStream = new FileStream(filePathName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFTP.Credentials = new NetworkCredential(clientUpdateInfo.FtpUserName, clientUpdateInfo.FtpUserPwd);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
Int bufferSize = 1024;
Int readCount;
Byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
/ / The size of the file on FTP
Int allbye = GetFtpFileSize(filename);// (int)response.ContentLength;
Int startbye = 0;
this.BeginInvoke(new MethodInvoker(delegate()
{
this.prog.Maximum = allbye;
this.prog.Minimum = 0;
this.prog.Visible = true;
this.lbDownInfo.Visible = true;
}));
While (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
Startbye += readCount;
this.BeginInvoke(new MethodInvoker(delegate()
{
this.lbDownInfo.Text = "Downloaded:" + (int)(startbye / 1024) + "KB/" + "Total length:"
+ (int)(allbye / 1024) + "KB" + " " + " filename:" + filename;
prog.Value = startbye;
this.lbDownInfo.Refresh();
}));
Application.DoEvents();
Thread.Sleep(5);
}
this.BeginInvoke(new MethodInvoker(delegate()
{
this.prog.Visible = false;
this.lbDownInfo.Text = "Download successful!";
}));
ftpStream.Close();
outputStream.Close();
response.Close();
}
C#ftp download file has a remote server return error: (500) syntax error, command not recognized