views:

2285

answers:

1

To get the file size for every file in a list of files, I'm using the following code:

foreach (String f in files)
{
  UriBuilder ftpUri = new UriBuilder("ftp", ftpServer, -1, ftpPfadZuLogDateien + "/" + f);
  FtpWebRequest ftpclientRequest1 = (FtpWebRequest)WebRequest.Create(ftpUri.Uri);
  ftpclientRequest1.Method = WebRequestMethods.Ftp.GetFileSize;
  ftpclientRequest1.Credentials = new NetworkCredential(ftpLoginName, ftpPassword);
  FtpWebResponse response1 = (FtpWebResponse)ftpclientRequest1.GetResponse();
  long filesize = response1.ContentLength;
  response1.Close();
  // store the file size somewhere
}

If there are only a few files in the list, this usually works. But after some of these requests (sometimes 10, sometimes 100) in a row, GetResponse() will throw an error 503 (Bad Sequence of Commands).

What is this error trying to tell me? Am I querying too fast? Forgetting to clean up any resource?
And what can I do about this?

additional info:
Setting KeepAlive=false on the connection makes it fail on the second request with error 550 (file not found/access denied?).
Setting UsePassive=false did not change anything.
Setting UseBinary=true did not change anything.
Hitting my head on the keyboard did not change anything.

[Update] beckr.org provided an answer - since it is hidden away behind a link and a lot of text, here the short version: i changed the source, so I'd reuse the NetworkCredentials:

NetworkCredential myCredentials = new NetworkCredential(ftpLoginName, ftpPassword);
foreach (String f in files)
{
  UriBuilder ftpUri = new UriBuilder("ftp", ftpServer, -1, ftpPfadZuLogDateien + "/" + f);
  FtpWebRequest ftpclientRequest1 = (FtpWebRequest)WebRequest.Create(ftpUri.Uri);
  ftpclientRequest1.Method = WebRequestMethods.Ftp.GetFileSize;
  ftpclientRequest1.Credentials = myCredentials;
  FtpWebResponse response1 = (FtpWebResponse)ftpclientRequest1.GetResponse();
  long filesize = response1.ContentLength;
  response1.Close();
  // store the file size somewhere
}

This way everything works like it should.

+2  A: 

Do a trace using Wireshark - the status code is 503, but the message is more likely something different. In my case, FtpWebRequest was re-authenticating every 3 requests or so with USER xyz, which resulted in the server (ProFTPD) replying "503 You are already logged in", which causes FtpWebRequest to bail out. See Feedback by Microsoft on why this happens at http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/9f0510f9-fa39-4aa4-8bd5-afb926282144/. If Microsoft would (properly) open source things, I wouldn't have an issue with this bug because I could just fix it myself, provide a patch and it would most likely go into further releases. But that's not how things work at Microsoft. Now looking into http://sourceforge.net/projects/dotnetftpclient/.

Using the same NetworkCredential over and over again did the trick!
Sam
Oh I didn't notice the solution at that link... happy that it worked for you!