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

简介: 如果下载多个文件的时候,有时候莫名其妙的出现500服务器错误,很有可能是没有设置KeepAlive 属性导致的。 出现应用程序未处理的异常:2015/1/6 11:40:56 异常类型:WebException 异常消息:远程服务器返回错误: (500) 语法错误,无法识别命令。

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

 

目录
相关文章
|
1月前
|
存储 安全 Shell
⭐⭐【Shell 命令集合 文件传输 】Linux ftp工具 使用指南
⭐⭐【Shell 命令集合 文件传输 】Linux ftp工具 使用指南
44 0
|
1月前
|
存储 弹性计算 数据可视化
要将ECS中的文件直接传输到阿里云网盘与相册(
【2月更文挑战第31天】要将ECS中的文件直接传输到阿里云网盘与相册(
420 4
|
1月前
|
SQL 分布式计算 DataWorks
DataWorks常见问题之dataworks连接FTP服务器失败如何解决
DataWorks是阿里云提供的一站式大数据开发与管理平台,支持数据集成、数据开发、数据治理等功能;在本汇总中,我们梳理了DataWorks产品在使用过程中经常遇到的问题及解答,以助用户在数据处理和分析工作中提高效率,降低难度。
|
1月前
|
存储 Shell Linux
【Shell 命令集合 文件传输 FTP客户端工具】Linux ncftp 命令使用指南
【Shell 命令集合 文件传输 FTP客户端工具】Linux ncftp 命令使用指南
40 0
|
1月前
|
缓存 网络协议 数据可视化
WinSCP下载安装并实现远程SSH本地服务器上传文件
WinSCP下载安装并实现远程SSH本地服务器上传文件
|
1月前
|
Linux 网络安全 Python
如何在服务器上运行python文件
如何在服务器上运行python文件
|
1月前
|
安全 数据处理 C#
C# Post数据或文件到指定的服务器进行接收
C# Post数据或文件到指定的服务器进行接收
|
1月前
|
Shell Python Windows
通过Python实现win11环境下FTP的上传与下载
通过Python实现win11环境下FTP的上传与下载
|
1月前
|
存储 网络协议 安全
如何搭建外网可访问的Serv-U FTP服务器,轻松远程共享文件!
如何搭建外网可访问的Serv-U FTP服务器,轻松远程共享文件!
|
1月前
|
监控 安全 测试技术
使用pyftpdlib组件实现FTP文件共享
使用pyftpdlib组件实现FTP文件共享
29 0