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

布满荆棘的人生 2022-01-31 07:19 562阅读 0赞

如果下载多个文件的时候,有时候莫名其妙的出现500服务器错误,很有可能是没有设置KeepAlive 属性导致的。

出现应用程序未处理的异常:2015/1/6 11:40:56
异常类型:WebException
异常消息:远程服务器返回错误: (500) 语法错误,无法识别命令。

参考:http://www.cnblogs.com/webabcd/archive/2007/01/21/626242.html

KeepAlive - 指定连接是应该关闭还是在请求完成之后关闭,默认为true

  1. /// <summary>
  2. /// FTP下载文件(带进度条)
  3. /// </summary>
  4. /// <param name="filename"></param>
  5. public void DownloadFile(string filename)
  6. {
  7. float percent = 0;
  8. string filePathName = string.Empty;
  9. string url = string.Empty;
  10. filePathName = Path.Combine(Application.StartupPath, filename);
  11. string dirPath = GetDirPath(filePathName);
  12. if (!Directory.Exists(dirPath))
  13. Directory.CreateDirectory(dirPath);
  14. //=>替换文件目录中的路径为网络路径
  15. filename = filename.Replace("\\", "/");
  16. url = "ftp://" + clientUpdateInfo.UpdateFTPIP + "/" + clientUpdateInfo.UpdatePath + "/" + filename;
  17. var reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  18. reqFtp.Method = WebRequestMethods.Ftp.DownloadFile;
  19. reqFtp.UseBinary = true;
  20. reqFtp.KeepAlive = false;//一定要设置此属性,否则一次性下载多个文件的时候,会出现异常。
  21. reqFtp.Credentials = new NetworkCredential(clientUpdateInfo.FtpUserName, clientUpdateInfo.FtpUserPwd);
  22. var response = (FtpWebResponse)reqFtp.GetResponse();
  23. long totalBytes = response.ContentLength;
  24. if (prog != null)
  25. {
  26. this.BeginInvoke(new MethodInvoker(delegate()
  27. {
  28. prog.Maximum = (int)totalBytes;
  29. }));
  30. }
  31. Stream st = response.GetResponseStream();
  32. var so = new FileStream(filePathName, FileMode.Create);
  33. long totalDownloadedByte = 0;
  34. byte[] by = new byte[1024];
  35. int osize = st.Read(by, 0, (int)by.Length);
  36. while (osize > 0)
  37. {
  38. totalDownloadedByte = osize + totalDownloadedByte;
  39. so.Write(by, 0, osize);
  40. if (prog != null)
  41. {
  42. this.BeginInvoke(new MethodInvoker(delegate()
  43. {
  44. prog.Value = (int)totalDownloadedByte;
  45. }));
  46. }
  47. osize = st.Read(by, 0, (int)by.Length);
  48. percent = (float)totalDownloadedByte * 1.0f / (float)totalBytes * 100;
  49. Application.DoEvents();
  50. this.BeginInvoke(new MethodInvoker(delegate()
  51. {
  52. lbDownInfo.Text = "正在下载" + filename + ",下载进度为:" + Math.Round(percent, 2) + "%";
  53. lbDownInfo.Refresh();
  54. }));
  55. Application.DoEvents();
  56. }
  57. so.Close();
  58. st.Close();
  59. response.Close();
  60. }
  61. private void FtpDownload(string filename)
  62. {
  63. string filePathName = string.Empty;
  64. string url = string.Empty;
  65. filePathName = Path.Combine(Application.StartupPath, filename);
  66. string dirPath = GetDirPath(filePathName);
  67. if (!Directory.Exists(dirPath))
  68. Directory.CreateDirectory(dirPath);
  69. //=>替换文件目录中的路径为网络路径
  70. filename = filename.Replace("\\", "/");
  71. url = "ftp://" + clientUpdateInfo.UpdateFTPIP + "/" + clientUpdateInfo.UpdatePath + "/" + filename;
  72. FtpWebRequest reqFTP;
  73. this.BeginInvoke(new MethodInvoker(delegate()
  74. {
  75. this.lbDownInfo.Text = "开始下载中...";
  76. }));
  77. FileStream outputStream = new FileStream(filePathName, FileMode.Create);
  78. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  79. reqFTP.Credentials = new NetworkCredential(clientUpdateInfo.FtpUserName, clientUpdateInfo.FtpUserPwd);
  80. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  81. reqFTP.UseBinary = true;
  82. reqFTP.KeepAlive = false;
  83. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  84. Stream ftpStream = response.GetResponseStream();
  85. int bufferSize = 1024;
  86. int readCount;
  87. byte[] buffer = new byte[bufferSize];
  88. readCount = ftpStream.Read(buffer, 0, bufferSize);
  89. //FTP上文件的大小
  90. int allbye = GetFtpFileSize(filename);// (int)response.ContentLength;
  91. int startbye = 0;
  92. this.BeginInvoke(new MethodInvoker(delegate()
  93. {
  94. this.prog.Maximum = allbye;
  95. this.prog.Minimum = 0;
  96. this.prog.Visible = true;
  97. this.lbDownInfo.Visible = true;
  98. }));
  99. while (readCount > 0)
  100. {
  101. outputStream.Write(buffer, 0, readCount);
  102. readCount = ftpStream.Read(buffer, 0, bufferSize);
  103. startbye += readCount;
  104. this.BeginInvoke(new MethodInvoker(delegate()
  105. {
  106. this.lbDownInfo.Text = "已下载:" + (int)(startbye / 1024) + "KB/" + "总长度:"
  107. + (int)(allbye / 1024) + "KB" + " " + " 文件名:" + filename;
  108. prog.Value = startbye;
  109. this.lbDownInfo.Refresh();
  110. }));
  111. Application.DoEvents();
  112. Thread.Sleep(5);
  113. }
  114. this.BeginInvoke(new MethodInvoker(delegate()
  115. {
  116. this.prog.Visible = false;
  117. this.lbDownInfo.Text = "下载成功!";
  118. }));
  119. ftpStream.Close();
  120. outputStream.Close();
  121. response.Close();
  122. }

发表评论

表情:
评论列表 (有 0 条评论,562人围观)

还没有评论,来说两句吧...

相关阅读