use*_*717 6 c# ftp ftp-client ftpwebrequest
我创建了一个ftp客户端,它在白天连接了几次,从FTP服务器检索日志文件.
问题是几个小时后我收到来自FTP服务器的错误消息(达到-421会话限制..).当我检查与netstat的连接时,我可以看到几个'ESTABLISHED'连接到服务器,即使我已"关闭"连接.
当我尝试通过命令行或FileZilla执行相同操作时,连接已正确关闭.
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
Run Code Online (Sandbox Code Playgroud)
如何正确关闭/断开连接?我忘了什么吗?
Jam*_*eck 10
尝试将FtpWebRequest.KeepAlive属性设置为false.如果KeepAlive设置为false,则在请求完成时将关闭与服务器的控制连接.
ftpWebRequest.KeepAlive = false;
Run Code Online (Sandbox Code Playgroud)