How to properly disconnect from FTP server with FtpWebRequest(如何使用 FtpWebRequest 正确断开与 FTP 服务器的连接)
问题描述
我创建了一个 ftp 客户端,它在一天中连接数次以从 FTP 服务器检索日志文件.
I've created a ftp client that connects several times during the day to retrieve log files from a FTP server.
问题是几个小时后,我从 FTP 服务器收到一条错误消息(已达到 -421 会话限制..).当我使用 netstat 检查连接时,我可以看到到服务器的多个已建立"连接,即使我已经关闭"了连接.
The Problem is that after a few hours I am getting an error message from the FTP server (-421 session limit reached..). When I check the connections with netstat, I can see several 'ESTABLISHED' connections to the server even though I've "closed" the connection.
当我尝试通过命令行或 FileZilla 执行相同操作时,连接已正确关闭.
When I try to do the same over the command line or FileZilla, the connections are properly closed.
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;
如何正确关闭/断开连接?我是不是忘了什么?
How can I close/disconnect the connection properly? Did I forget anything?
推荐答案
尝试设置 FtpWebRequest.KeepAlive 属性为 false.如果 KeepAlive
设置为 false,则在请求完成时将关闭与服务器的控制连接.
Try and set the FtpWebRequest.KeepAlive property to false. If KeepAlive
is set to false, then the control connection to the server will be closed when the request completes.
ftpWebRequest.KeepAlive = false;
这篇关于如何使用 FtpWebRequest 正确断开与 FTP 服务器的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 FtpWebRequest 正确断开与 FTP 服务器的连接


基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01