【问题标题】:The underlying connection was closed: The server committed a protocol violation. FTP底层连接已关闭:服务器违反了协议。 FTP
【发布时间】:2012-03-22 03:12:02
【问题描述】:

我正在使用下面的代码上传我的文件,它工作正常,但有时它会抛出此错误“底层连接已关闭:服务器违反协议”并停止进程,当我再次运行它时,它会上传文件解决任何问题。

我注意到的一件事,如果我运行此过程来上传多个文件,那么有时我会收到此错误,如果我执行的文件少于 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;为“真”以消除我的错误。

【问题讨论】:

    标签: c#


    【解决方案1】:

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

    【讨论】:

    • 我试过了,但仍然遇到同样的错误。有什么想法还有什么问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    相关资源
    最近更新 更多