底层连接已关闭:服务器违反协议。 FTP

底层连接已关闭:服务器违反协议。 FTP

问题描述:

我上传我的文件使用下面的代码,它工作正常,但一段时间它引发此错误“底层连接已关闭:服务器提交协议违规”,并停止进程,当我再次运行它上传文件与解决任何问题。底层连接已关闭:服务器违反协议。 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;到“真”来摆脱我的错误。

+0

更改此行以摆脱错误“reqFTP.KeepAlive = True;” – Usher 2012-03-27 23:39:51

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

+0

我试过了,仍然有同样的错误。任何想法还有什么可能是这个问题? – 2016-07-24 17:08:40