来自FTPWebRequest.GetResponse()方法的状态代码

时间:2022-10-23 19:09:56

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.

我正在异步上传文件到FTP。上传每个文件后,我正在检查该文件的上传操作的状态。这可以使用该请求的FtpWebResponse对象的StatusCode属性来完成。代码段如下所示。

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 :

根据msdn,大约有37个StatusCode值。我不知道这些状态代码值中的哪一个将确保文件成功上传。我在代码中用于检查成功的其中一些是:

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.

但我不知道其余的。我需要确定这些代码,因为基于上传操作的失败或成功,我还要执行其他相关操作。错误的条件会影响剩余的代码。我想到的另一个想法是简单地将上面的代码放入try..catch而不依赖于这些状态代码。有了这个,我不会依赖于状态代码,并假设任何失败将始终指向catch块。如果这是正确的方法,请告诉我。

1 个解决方案

#1


6  

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

FtpStatusCode.ConnectionClosed是426,它是Connection关闭的;转移中止所以我认为这实际上是一个失败。 2XX范围内的任何东西通常都应该是成功的。对于我构建的FTP客户端,我记得收到成功上传的那个是226 - FtpStatusCode.ClosingData

#1


6  

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

FtpStatusCode.ConnectionClosed是426,它是Connection关闭的;转移中止所以我认为这实际上是一个失败。 2XX范围内的任何东西通常都应该是成功的。对于我构建的FTP客户端,我记得收到成功上传的那个是226 - FtpStatusCode.ClosingData