底层连接已关闭:服务器提交了协议违规。(The underlying connection was closed: The server committed a protocol violation.

编程入门 行业动态 更新时间:2024-10-21 20:34:57
底层连接已关闭:服务器提交了协议违规。(The underlying connection was closed: The server committed a protocol violation. FTP)

我使用下面的代码上传我的文件,它工作正常,但有时它抛出此错误“基础连接已关闭:服务器提交了协议违规”并停止该过程,当我再次运行它上传文件没有任何问题。

有一点我注意到,如果我运行这个过程来上传多个文件,那么我有时会得到这个错误,如果我做少于5个文件然后它工作正常,任何想法在哪里和我必须看到它。

我搜索谷歌没有找到可靠的解决方案,甚至其中一个msdn博客说它在FTPwebrequest中的错误但不确定。

环境:C#4.0,IIS FTP服务器。

提前致谢

FileInfo fileInf = new FileInfo(filename); FtpWebRequest reqFTP; // Create FtpWebRequest object from the Uri provided reqFTP = (FtpWebRequest)FtpWebRequest.Create (new Uri(path + fileInf.Name)); // Provide the WebPermission Credintials reqFTP.Credentials = new NetworkCredential(user, pwd); // By default KeepAlive is true, where the control connection // is not closed after a command is executed. reqFTP.KeepAlive = false; // Specify the command to be executed. reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // Specify the data transfer type. reqFTP.UseBinary = true; reqFTP.Timeout = -1; reqFTP.UsePassive = true; // Notify the server about the size of the uploaded file reqFTP.ContentLength = fileInf.Length; // The buffer size is set to 2kb int buffLength = 4096; byte[] buff = new byte[buffLength]; int contentLen; // Opens a file stream (System.IO.FileStream) to read the file // to be uploaded FileStream fs = fileInf.OpenRead(); try { // Stream to which the file to be upload is written Stream strm = reqFTP.GetRequestStream(); // Read from the file stream 2kb at a time contentLen = fs.Read(buff, 0, buffLength); // Till Stream content ends while (contentLen != 0) { // Write Content from the file stream to the FTP Upload // Stream strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } // Close the file stream and the Request Stream strm.Close(); fs.Close(); }

回答

reqFTP.KeepAlive = false; 以“真”来摆脱我的错误。

I was uploading my file using below code,it works fine but some time it throws this error"The underlying connection was closed: The server committed a protocol violation" and stop the process ,when i run it again it upload file with out any issue.

One thing i noticed,if i run this process to upload multiple files then i get this error sometime,if i do less than 5 files then it works fine,any idea where and what i have to look in to it.

I was searchin google nothing found solid solution for this,even one of the msdn blog saying its a bug in FTPwebrequest but not sure.

Environment : C# 4.0, IIS FTP Server.

Thanks in Advance

FileInfo fileInf = new FileInfo(filename); FtpWebRequest reqFTP; // Create FtpWebRequest object from the Uri provided reqFTP = (FtpWebRequest)FtpWebRequest.Create (new Uri(path + fileInf.Name)); // Provide the WebPermission Credintials reqFTP.Credentials = new NetworkCredential(user, pwd); // By default KeepAlive is true, where the control connection // is not closed after a command is executed. reqFTP.KeepAlive = false; // Specify the command to be executed. reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // Specify the data transfer type. reqFTP.UseBinary = true; reqFTP.Timeout = -1; reqFTP.UsePassive = true; // Notify the server about the size of the uploaded file reqFTP.ContentLength = fileInf.Length; // The buffer size is set to 2kb int buffLength = 4096; byte[] buff = new byte[buffLength]; int contentLen; // Opens a file stream (System.IO.FileStream) to read the file // to be uploaded FileStream fs = fileInf.OpenRead(); try { // Stream to which the file to be upload is written Stream strm = reqFTP.GetRequestStream(); // Read from the file stream 2kb at a time contentLen = fs.Read(buff, 0, buffLength); // Till Stream content ends while (contentLen != 0) { // Write Content from the file stream to the FTP Upload // Stream strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } // Close the file stream and the Request Stream strm.Close(); fs.Close(); }

Answer

reqFTP.KeepAlive = false; to "True" to get rid of the error in my case.

最满意答案

从reqFTP.KeepAlive = false更改; to“reqFTP.KeepAlive = True;” 摆脱错误

Changed from reqFTP.KeepAlive = false; to "reqFTP.KeepAlive = True;" to get rid of the error

更多推荐

本文发布于:2023-04-28 00:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:底层   协议   服务器   connection   underlying

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!