Austin Salonen
Austin Salonen

Reputation: 50215

.Net: FtpWebRequest "(503) Bad sequence of commands" error

On large files (~200+ MB), I get the 503 error when I read the stream.

ftp = (FtpWebRequest)WebRequest.Create(new Uri(address.AbsoluteUri + @"/" + file.Name));
ftp.Credentials = new NetworkCredential(username, password);
ftp.Method = WebRequestMethods.Ftp.DownloadFile;

response = (FtpWebResponse)ftp.GetResponse();

Any clues on what I'm doing wrong or a better practice for larger files?

Upvotes: 6

Views: 10010

Answers (5)

mrrrk
mrrrk

Reputation: 2301

In my case, the server (belonging to a client) had been changed to only accept TLS (SSL) requests. They didn't tell us this of course and the 503 error message was not helpful!

So we needed to use .EnableSsl like this:

        var ftp = WebRequest.Create(uri) as FtpWebRequest;
        if (ftp != null) {
            ftp.EnableSsl = true; // <- the new bit
            ftp.Credentials = myCredentials;
            ftp.KeepAlive = false; // <- you may or may not want this
        }
        return ftp;

In order to ignore certificate errors, I also needed to add this:

System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                (sender, certificate, chain, sslPolicyErrors) => true;

It's my understanding that this line has a global effect, so just need to call it when your application starts.

:o)

Upvotes: 1

Mike Miller
Mike Miller

Reputation: 11

The error is due to not allowing time for the server to log out. Use a try statement and when this error occurs, createa short time delay and have it start over. This will allow the server to logout from the last rrequest.

Upvotes: 1

bmacadam
bmacadam

Reputation: 194

I'd concur that minimizing the number of NetworkCredential objects solves the problem. I experienced this problem and created a single CredentialCache and added each credential once. No more ftp errors.

Upvotes: 4

adzm
adzm

Reputation: 4136

For some reason, several people seem to have noticed some success with using only one instance of a NetworkCredential object rather than using a new one for each FtpWebRequest. This worked for me, at least.

Upvotes: 3

John Lemp
John Lemp

Reputation: 5067

Do you receive the 503 after every attempt or only subsequent attempts?

Have you tried setting the disabling KeepAlive?

ftp.KeepAlive = false;

I would try a more rubust ftp client library, a basic free one can be at sourceforge.

Upvotes: 4

Related Questions