Status Code from FTPWebRequest.GetResponse() method

Nishant picture Nishant · Jan 17, 2011 · Viewed 10k times · Source

This is slightly tricky.

I am uploading files to FTP asynchronously. After uploading each file I am checking the status of the upload operation for that file. This can be done with StatusCode property of the FtpWebResponse object for that request. The code snippet is as give below.

FileStream fs = File.Open(fileName, FileMode.Open);

while ((iWork = fs.Read(buf, 0, buf.Length)) > 0)
    requestStream.Write(buf, 0, iWork);

requestStream.Close();

FtpWebResponse wrRet = ((FtpWebResponse)state.Request.GetResponse());

There are about 37 StatusCode values as per msdn. I am unaware as to which of these status code values will assure that the file is uploaded successfully. Some of them I used in my code to check for success are :

wrRet.StatusCode == FtpStatusCode.CommandOK 
wrRet.StatusCode == FtpStatusCode.ClosingData
wrRet.StatusCode == FtpStatusCode.ClosingControl
wrRet.StatusCode == FtpStatusCode.ConnectionClosed
wrRet.StatusCode == FtpStatusCode.FileActionOK
wrRet.StatusCode == FtpStatusCode.FileStatus

But I am unaware of the rest. I need to be sure about these codes because based on the failure or success of the upload operation I have other dependent operations to be carried out. A wrong condition can affect the remaining code. Another thought that crossed my mind was to simply put the above code into a try..catch and not depend on these status codes. With this I would not be depending on the status codes and assuming that any failure will always be directed to the catch block. Kindly let me know if this is the right way.

Answer

Chris Haas picture Chris Haas · Jan 17, 2011

FtpStatusCode.ConnectionClosed is 426 which is Connection closed; transfer aborted so I would think that would be a failure actually. Anything in the 2XX range should generally be a success. For the FTP clients that I've built the one that I only remember receiving for successful upload is 226 - FtpStatusCode.ClosingData