如果下载多个文件的时候,有时候莫名其妙的出现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. PHP Framework安装

    Framework 1> 初始化 前提:服务器上已经装有 Apache/Nginx 和 MySQL 进入 hush-framework/hush-app/bin 目录(Linux 下需执行 ch ...

  2. Linux共享内存(一)

    inux系统编程我一直看 <GNU/LINUX编程指南>,只是讲的太简单了,通常是书和网络上的资料结合着来掌握才比较全面 .在掌握了书上的内容后,再来都其他资料 . 原文链接 http:/ ...

  3. MYSQL中 ENUM、SET 类型(建议用tinyint代替)

    ENUM类型 ENUM 是一个字符串对象,其值通常选自一个允许值列表中,该列表在表创建时的列规格说明中被明确地列举. 在下列某些情况下,值也可以是空串("") 或 NULL: 如果 ...

  4. Golang 绘图技术(image/draw包介绍)

          image/draw 包仅仅定义了一个操作:通过可选的蒙版图(mask image),把一个原始图片绘制到目标图片上,这个操作是出奇的灵活,可以优雅和高效的执行很多常见的图像处理任务. 1 ...

  5. python发布模块的原理及部分讲解

  6. JS代码的简单重构与优化

    JS代码的简单重构与优化(适合新手) 原文  http://www.cnblogs.com/similar/p/5016424.html Demo . 1 //bad if (age > 20) ...

  7. python中的文件

    Python文件 1.    概述 文件对象不仅可以用来访问普通的磁盘文件,也可以访问任何其他类型抽象层面上的文件. 内建函数open()以及file()提供了初始化输入输出(I/O)操作的通用接口. ...

  8. (转载)OC学习篇之---Foundation框架中的NSDirctionary类以及NSMutableDirctionary类

    昨天学习了Foundation框架中NSArray类和NSMutableArray类,今天来看一下Foundation框架中的NSDirctionary类,NSMutableDirctionary类, ...

  9. ps制作哈7海报字体

    模仿也需要较强的功底和分析思路.如下面的教程,作者模仿的是电影海报字.文字构造虽不复杂,不过思路不对的话就容易走弯路.最终效果 1.先来分析文字的构造,大致由两部分组成,一部分是浮雕字,另一部分是质感 ...

  10. Hibernate中openSession() 与 getCurrentSession()的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...