7

I want to loop on a FTP folder for check if a file has arrived

I do:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost:8080");
request.Credentials = new NetworkCredential("anonymous", "");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

while(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();
    }
}

But at the second iteration I get an exception:

The stream cannot be read

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

2 Answers2

4

Sorry, I missed it, you're only issuing one request and trying to get a response multiple times. Try the code below:

while(true)
{
    FtpWebRequest request =     (FtpWebRequest)WebRequest.Create("ftp://localhost:8080");
    request.Credentials = new NetworkCredential("anonymous", "");
    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

    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();
    }
}

You should add a pause of some sort at the end of each loop though. You don't want to bombard the server.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Peter4499
  • 685
  • 7
  • 15
  • Yes , It works but I have to login again every time? –  Jun 04 '16 at 22:28
  • 1
    Each request is a separate entity. It's not really logging in, it's providing identification as to who is making the request. The `FtpWebRequest` class is a wrapper for Ftp calls but still uses web methodology, each request is it's own entity. – Peter4499 Jun 04 '16 at 22:33
4

You cannot reuse the FtpWebRequest instance for multiple requests.

But as the FtpWebRequest works on top of a connection pool, it actually can reuse an underlying FTP connection. As long as the FtpWebRequest.KeepAlive is set to its default value of true.

When the KeepAlive is set to true, the underlying FTP connection is not closed, when the request finishes. When you create another instance of the FtpWebRequest with the same URL, the connection is 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();
    }
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992