C#FTP下载文件出现远程服务器返回错误: (500) 语法错误,无法识别命令

时间:2023-03-08 20:35:09

如果下载多个文件的时候,有时候莫名其妙的出现500服务器错误,很有可能是没有设置KeepAlive 属性导致的。

出现应用程序未处理的异常:2015/1/6 11:40:56
异常类型:WebException

异常消息:远程服务器返回错误: (500) 语法错误,无法识别命令。

参考:http://www.cnblogs.com/webabcd/archive/2007/01/21/626242.html

KeepAlive - 指定连接是应该关闭还是在请求完成之后关闭,默认为true

 /// <summary>
/// FTP下载文件(带进度条)
/// </summary>
/// <param name="filename"></param>
public void DownloadFile(string filename)
{
float percent = ;
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);
//=>替换文件目录中的路径为网络路径
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;//一定要设置此属性,否则一次性下载多个文件的时候,会出现异常。
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 = ;
byte[] by = new byte[];
int osize = st.Read(by, , (int)by.Length);
while (osize > )
{
totalDownloadedByte = osize + totalDownloadedByte;
so.Write(by, , osize);
if (prog != null)
{
this.BeginInvoke(new MethodInvoker(delegate()
{
prog.Value = (int)totalDownloadedByte;
})); }
osize = st.Read(by, , (int)by.Length);
percent = (float)totalDownloadedByte * 1.0f / (float)totalBytes * ;
Application.DoEvents();
this.BeginInvoke(new MethodInvoker(delegate()
{
lbDownInfo.Text = "正在下载" + filename + ",下载进度为:" + Math.Round(percent, ) + "%";
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);
//=>替换文件目录中的路径为网络路径
filename = filename.Replace("\\", "/");
url = "ftp://" + clientUpdateInfo.UpdateFTPIP + "/" + clientUpdateInfo.UpdatePath + "/" + filename;
FtpWebRequest reqFTP;
this.BeginInvoke(new MethodInvoker(delegate()
{
this.lbDownInfo.Text = "开始下载中...";
}));
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 = ;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, , bufferSize);
//FTP上文件的大小
int allbye = GetFtpFileSize(filename);// (int)response.ContentLength;
int startbye = ;
this.BeginInvoke(new MethodInvoker(delegate()
{
this.prog.Maximum = allbye;
this.prog.Minimum = ;
this.prog.Visible = true;
this.lbDownInfo.Visible = true;
}));
while (readCount > )
{
outputStream.Write(buffer, , readCount);
readCount = ftpStream.Read(buffer, , bufferSize);
startbye += readCount;
this.BeginInvoke(new MethodInvoker(delegate()
{
this.lbDownInfo.Text = "已下载:" + (int)(startbye / ) + "KB/" + "总长度:"
+ (int)(allbye / ) + "KB" + " " + " 文件名:" + filename;
prog.Value = startbye;
this.lbDownInfo.Refresh();
}));
Application.DoEvents();
Thread.Sleep();
}
this.BeginInvoke(new MethodInvoker(delegate()
{
this.prog.Visible = false;
this.lbDownInfo.Text = "下载成功!";
}));
ftpStream.Close();
outputStream.Close();
response.Close();
}