C# 基础连接已关闭:服务器违反了协议。文件传输协议
我用下面的代码上传我的文件,它工作正常,但有一段时间它抛出了这个错误“底层连接已关闭:服务器违反了协议”,并停止了这个过程,当我再次运行它时,它上传文件没有任何问题 我注意到的一件事是,如果我运行这个过程来上传多个文件,那么我会在某个时候出现这个错误,如果我上传的文件少于5个,那么它就可以正常工作,知道我在哪里以及需要查看什么 我在谷歌搜索,没有找到解决这个问题的可靠方法,甚至有一个msdn博客说这是FTPWebreRequest中的一个bug,但不确定 环境:C#4.0,IIS FTP服务器 提前谢谢C# 基础连接已关闭:服务器违反了协议。文件传输协议,c#,C#,我用下面的代码上传我的文件,它工作正常,但有一段时间它抛出了这个错误“底层连接已关闭:服务器违反了协议”,并停止了这个过程,当我再次运行它时,它上传文件没有任何问题 我注意到的一件事是,如果我运行这个过程来上传多个文件,那么我会在某个时候出现这个错误,如果我上传的文件少于5个,那么它就可以正常工作,知道我在哪里以及需要查看什么 我在谷歌搜索,没有找到解决这个问题的可靠方法,甚至有一个msdn博客说这是FTPWebreRequest中的一个bug,但不确定 环境: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;设置为“True”以消除我的案例中的错误。已从reqFTP.KeepAlive更改为false;为了“reqFTP.KeepAlive=True;”为了消除错误我试过了,但仍然得到了相同的错误。你知道还有什么问题吗?