You cannot reuse an instance of FtpWebRequest for multiple requests.
But since FtpWebRequest runs on top of the connection pool, it can actually reuse the underlying FTP connection. As long as FtpWebRequest.KeepAlive set, the default value is true .
If KeepAlive set to true , the underlying FTP connection does not close when the request completes. When you create another instance of FtpWebRequest with the same URL, the connection will be reused.
while (true) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost:8080"); request.Credentials = new NetworkCredential("anonymous", ""); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; // reuse the connection (not necessary, as the true is the default) request.KeepAlive = true; using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) { Console.WriteLine(reader.ReadToEnd()); reader.Close(); response.Close(); } }
source share