Status Code from FTPWebRequest GetResponse method

Posted by nick on Stack Overflow See other posts from Stack Overflow or by nick
Published on 2011-01-17T13:42:09Z Indexed on 2011/01/17 13:53 UTC
Read the original article Hit count: 383

Filed under:
|

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.

System.IO.FileStream fs = System.IO.File.Open(fileName, System.IO.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 dependant 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.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about ftpwebresponse