如果下载多个文件的时候,有时候莫名其妙的出现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 = 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);//=>替换文件目录中的路径为网络路径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 = 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 = "正在下载" + filename + ",下载进度为:" + 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);//=>替换文件目录中的路径为网络路径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 = 1024;int readCount;byte[] buffer = new byte[bufferSize];readCount = ftpStream.Read(buffer, 0, bufferSize);//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 = "已下载:" + (int)(startbye / 1024) + "KB/" + "总长度:"+ (int)(allbye / 1024) + "KB" + " " + " 文件名:" + filename;prog.Value = startbye;this.lbDownInfo.Refresh();}));Application.DoEvents();Thread.Sleep(5);}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)文件不可用(例如,未找到文件,无法访问文件)的解决方法

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

  2. 远程服务器返回urlto龙,,一个简单的Get请求 老是出现【远程服务器返回异常:501】错误...

    救命啊,一个简单的Get请求 老是出现[远程服务器返回错误:501]异常 就是Get签到这里出现了异常,其他的不会.网上说是将IIS的"Web服务扩展"中的"WebDAV ...

  3. FTP 传送文件到远程服务器

    1.连接到远程服务器 ftp 远程服务器ip //按照提示输入用户名及密码 2.传输文件 put 绝对路径+要传输的文件 会被传到用户目录下,比如登录系统的用户名为jiankunking,则会被传输到 ...

  4. [Work Summary] 远程FTP下载文件到本地目录

    美图欣赏2022/06/07 工作过程中,客户将input数据存放在FTP服务器的情况,通过查阅相关资料了解可从远程FTP下载文件到本地的功能需求,特此记录以下内容方便日后查阅与学习 问题:客户每周周 ...

  5. 使用FTP下载文件connect.retrieveFileStream(filename) 获取不到InputStream流,返回null的问题

    使用同事的代码做FTP下载文件,InputStream in = connect.retrieveFileStream(fileName);执行这句时InputStream总是获取为空 后来把代码改成 ...

  6. linux中如何用ftp命令下载文件,linux中ftp下载文件命令的用法

    linxu下的ftp命令是用来下载文件或者上传文件的,下面由学习啦小编为大家整理了linux的ftp下载文件命令的用法的相关知识,希望对大家有帮助! 一.linux中的ftp下载文件命令的用法 从远程 ...

  7. 文件怎么上传远程服务器,怎么上传文件到远程服务器

    怎么上传文件到远程服务器 内容精选 换一换 将文件上传至Windows云服务器一般会采用MSTSC远程桌面连接的方式.本节为您介绍本地Windows计算机通过远程桌面连接,上传文件至Windows云服 ...

  8. scp复制本地文件到远程服务器,scp 本地文件到远程服务器

    linux中scp命令的使用 linux远程拷贝文件命令:scp(scp:secure corp)(1)从本地拷贝文件到远程服务器scp/opt/script/test.pl root@192.168 ...

  9. 向服务器上传文件的命令,上传文件到远程服务器的命令

    上传文件到远程服务器的命令 内容精选 换一换 将文件上传至Windows云服务器一般会采用MSTSC远程桌面连接的方式.本节为您介绍本地Windows计算机通过远程桌面连接,上传文件至Windows云 ...

最新文章

  1. 基于 RT-Thread赛车控制算法开发
  2. 归并排序及C语言实现
  3. 信息学奥赛一本通(C++)在线评测系统——基础(三)数据结构 —— 1363:小球(drop)
  4. FatFs最新版本获取方法
  5. Linux——VIM开发C++自动补全(STL、括号)和开发环境配置
  6. flask使用第三方云通讯平台时,出现{'172001':'网络错误'}解决方法
  7. Oracle11g新特性:在线操作功能增强-Oracle11g新增的不可见索引 (转载)
  8. 烫烫烫和屯屯屯2021
  9. 学计算机和电脑办公的区别,自学编程和计算机科班出身的差别在哪?
  10. Java开发两年:java简历包装工作经验
  11. 哈工大网络安全实验五报告
  12. 雷神开机logo更改_开机logo以及两种修改开机动画方法
  13. Alibaba微服务组件Nacos单机+集群配置 prometheus+grafana监控配置及注册中心实战【收获满满】
  14. 推荐系统-推荐模型总结
  15. 题6.12:有一行电文,已按照下面规律翻译成密码: A->Z a->z B->Y b->y C->X c->x即第1个字母编程第26个字母,第i个字母编程第(26-i+1)个字母,非字母字符不变,要求
  16. matlab——红绿灯颜色及数字识别(一)
  17. python编程一个正方体的代码_Linux Shell经典面试题之请用shell或Python编写一个正方形(square.sh),接受用户输入的数字...
  18. 计算机if语句翻译,5.4.1 if 语句的翻译
  19. 115846-45-2、二肽标记肽Suc-GP-对硝基苯胺、 Suc-Gly-Pro-pNA
  20. 把pdf转换成ppt的软件

热门文章

  1. 本周ASP.NET英文技术文章推荐[04/08 - 04/14](附赠自弹超级玛丽主题曲)
  2. TemplateBinding与Binding区别,以及WPF自定义控件开发的遭遇
  3. as3文本框的动态拖拽和编辑
  4. ​ArduinoYun教程之ArduinoYun硬件介绍
  5. 基于ip tunnel连接不同三个不同网络的×××
  6. 爱数应用容灾部署方案三
  7. 可爱的穆里尼奥,可爱的切尔西!
  8. ecshop模板支持php,[老杨原创]关于ECSHOP模板架设的服务器php版本过高报错的解决方法集合...
  9. query string parameters什么意思_public static void main(String[] args) 是什么意思?(转)...
  10. MySQL时间格式TIMESTAMP和DATETIME的区别