如果下载多个文件的时候,有时候莫名其妙的出现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();
}

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

  1. C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问文件)的解决方法

    最近在做项目的时候需要操作ftp进行文件的上传下载,但在调用using (var response = (FtpWebResponse)FtpWebRequest.GetResponse())的时候总 ...

  2. C# FTP远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)

    今天用代码删除FTP服务器上的目录时候,报错:远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件). 习惯性的google,不外乎以下几点: 1.URL路径不对,看看有没有多加 ...

  3. 写自动更新程序出现"远程服务器返回错误: (404) 未找到"

    在win2003配置后,在客户端运行时能够下载exe和dll文件,但是在更新lib文件时总是报“远程服务器返回错误: (404) 未找到”错误,不明白咋会出现这个问题,去网上一查,发现以下解决办法: ...

  4. .Net 连接FTP下载文件报错:System.InvalidOperationException: The requested FTP command is not supported when using HTTP proxy

    系统环境: Windows + .Net Framework 4.0   问题描述: C#连接FTP下载文件时,在部分电脑上有异常报错,在一部分电脑上是正常的:异常报错的信息:System.Inval ...

  5. 安装SSH2拓展 PHP上传文件到远程服务器

    情景:客户端上传图片到服务器A,服务器A同步上传至另外一个静态资源服务器B 环境:php7 linux(ubuntu) 安装php的ssh2扩展 -dev sudo apt-get install p ...

  6. C# 上传文件至远程服务器

    C# 上传文件至远程服务器(适用于桌面程序及web程序) 2009-12-30 19:21:28|  分类: C#|举报|字号 订阅     最近几天在玩桌面程序,在这里跟大家共享下如何将本地文件上传 ...

  7. system.net.webexception远程服务器返回了错误: NotFound。

    Not Found类的错误主要是由于网络服务访问出错.所以需要分析是由哪个网络服务访问失败而导致的. DataAccessSilverlight.PowerDataServiceReference.G ...

  8. fft 远程服务器返回错误 550返回码

    "远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)"时,可能是如下原因: 1.URL路径不对,看看有没有多加空格,或者大小写问题 2.权限是否足 3.需 ...

  9. SSH命令行传输文件到远程服务器

    Ubuntu操作系统 SCP命令 使用方式如下: 1.上传本地文件到远程服务器 scp /var/www/test.php root@192.168.0.101:/var/www/ 把本机/var/w ...

随机推荐

  1. ulimit 命令

    用途:ulimit用于shell启动进程所占用的资源. 类别:shell内建命令 语法格式:ulimit [-acdfHlmnpsStvw] [size] 参数: -H 设置硬资源限制. -S 设置软 ...

  2. 纠结的ARC

    xcode不断进步,在xcode4中引入了ARC的概念.您用或者不用它就在那里,于是有了本文:如何在未使用arc的工程中引入一个使用了arc特性的文件,如何在arc工程中引用未使用arc的文件.其实说 ...

  3. selenium python (六)定位一组对象

    checkbox源码: <html><head><meta http-equiv="content-type" content="text/ ...

  4. Nodejs_day01

    helloworld.txt的内容: 我是nodejs 阻塞式调用 var fs = require('fs'); var data = fs.readFileSync('helloworld.txt ...

  5. 代码以兼容高亮方式发布.xml

    函数名: abort  功  能: 异常终止一个进程  用  法: void abort(void);  程序例:  #include <stdio.h>  #include <st ...

  6. Linux操作系统中,.zip、.tar、.tar.gz、.tar.bz2、.tar.xz、.jar、.7z等格式的压缩与解压

    zip格式 压缩: zip -r [目标文件名].zip [原文件/目录名] 解压: unzip [原文件名].zip 注:-r参数代表递归 tar格式(该格式仅仅打包,不压缩) 打包:tar -cv ...

  7. C#开源框架(整理)

    http://www.cnblogs.com/chinanetwind/p/3715809.html http://www.cnblogs.com/chinanetwind/p/3715813.htm ...

  8. PHP 新建动态类的代码

    $testObject=(object)array(); $testObject->first="firstValue"; var_dump($testObject); $t ...

  9. 自己使用python webob,paste.deploy,wsgi总结

    paste.deploy就是一个可以配置wsgi_app的工具,可以让服务器运行时,按照配置文件执行一系列的程序.需要使用.ini配置文件. (1)这里补充一下当时没看到的配置文件 1.[app:ma ...

  10. Trie树也称字典树

    Trie树 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 一.Trie树的原理 利用串构建一个字典树,这个字典树保存了串的公共前缀信息, ...