日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/4 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#FTP下载文件出现远程服务器返回错误: (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();}

?

總結

以上是生活随笔為你收集整理的C#FTP下载文件出现远程服务器返回错误: (500) 语法错误,无法识别命令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。