urmm, hey guys.
im getting a 503 error on my program i'm making.
It's used to backup multiple files to a online ftp server.

and on the 5th file
it gets it on this line...

Stream requestStream = request.GetRequestStream();

heres my code (for the upload)...

public void upload(string pathtofile, string file)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/" + file);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential("username", "password");
            byte[] fileContents = File.ReadAllBytes(pathtofile + "\\" + file);

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            response.Close();
        }

and that's used with multiple commands like: upload("c:\\", "file.mdb")

is there any way of fixing this? thanks - maxicube

Recommended Answers

All 6 Replies

You're probably pissing the FTP server off since you appear to be hammering it with connections. You should re-use an FTP connection to send file after file on the same connection, meaning you should open the connection -- send file 1 -- send file 2 -- send file 3 -- send file N -- close connection.

[edit]
With the HTTP protocol you can re-use the same connection for multiple requests using HTTP Keep-Alive, but most applications use a new connection for every request.

With FTP this is not the case. It is considered abusive by most providers to hammer connections like this. I drop connections to my FTP if they come in too fast:

iptables -t nat -I PREROUTING 1 -d x.x.x.x -p tcp -m tcp --dport 21 -m state --state NEW -m recent --set --name FTP
iptables -t nat -I PREROUTING 2 -d x.x.x.x -p tcp -m tcp --dport 21 -m state --state NEW -m recent --update --name FTP --seconds 300 --hitcount 5 -j DROP

[/edit]

So your just saying. Either keep the connection open.
Or put a thread.sleep(1000) in between each upload?

No, i'm saying keep the connection open. Trying to figure out the extent to which you can hammer that poor guys server is not a solution ;)

Thanks for the reply. Umm I'm guessing just run the connect bit once. And put request.keepalive = true; in and make the close connection only happen once. I'll try this when I wake up.
Again. Thanks for your time.
Btw. I'm a bitof a newbie at c#. Only started the other day :D

Sounds good. According to MSDN that is the approach you should take.

Please mark this thread as solved once you have a chance to test the solution and good luck!

mmm, i found a site with an example.
but i realised request.KeepAlive was default to true
so i just set it to false and it seemed to love me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.