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

发布于 2024-10-12 03:10:03 字数 1050 浏览 7 评论 0原文

这有点棘手。

我正在异步上传文件到 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());

根据 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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-10-19 03:10:03

FtpStatusCode.ConnectionClosed426,表示连接已关闭;传输中止所以我认为这实际上是一个失败。 2XX 范围内的任何内容通常都应该是成功的。对于我构建的 FTP 客户端,我只记得成功上传时收到的客户端是 226 - FtpStatusCode.ClosingData

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文