我正在尝试使用 QT Network 实现 FTPClient 。
我该如何处理特殊情况,例如下载时网线被拔掉,没有互联网连接等。?
我的 FTPClient 怎么能知道这样的事件,是否有这样的通知可用?
我曾尝试使用 done(bool) 、 ommandFinished ( int id, bool error ) 之类的信号,但我没有收到任何类型的信号。
您似乎使用过时的 QFtp。您应该使用 QNetworkReply(和 QNetworkAccessManager),它已经完成()和错误()信号: QNetworkReply 文档。
您是否尝试过创建自定义 SLOT 并将其连接到 QNetworkReply错误信号?
然后,您可以检查参数以确定错误并决定如何处理它。
QNetworkReply::NoError 0 no error condition. Note: When the HTTP protocol returns a redirect no error will be reported. You can check if there is a redirect with the QNetworkRequest::RedirectionTargetAttribute attribute.
QNetworkReply::ConnectionRefusedError 1 the remote server refused the connection (the server is not accepting requests)
QNetworkReply::RemoteHostClosedError 2 the remote server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply::HostNotFoundError 3 the remote host name was not found (invalid hostname)
QNetworkReply::TimeoutError 4 the connection to the remote server timed out
QNetworkReply::OperationCanceledError 5 the operation was canceled via calls to abort() or close() before it was finished.
QNetworkReply::SslHandshakeFailedError 6 the SSL/TLS handshake failed and the encrypted channel could not be established. The sslErrors() signal should have been emitted.
QNetworkReply::TemporaryNetworkFailureError 7 the connection was broken due to disconnection from the network, however the system has initiated roaming to another access point. The request should be resubmitted and will be processed as soon as the connection is re-established.
QNetworkReply::ProxyConnectionRefusedError 101 the connection to the proxy server was refused (the proxy server is not accepting requests)
QNetworkReply::ProxyConnectionClosedError 102 the proxy server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply::ProxyNotFoundError 103 the proxy host name was not found (invalid proxy hostname)
QNetworkReply::ProxyTimeoutError 104 the connection to the proxy timed out or the proxy did not reply in time to the request sent
QNetworkReply::ProxyAuthenticationRequiredError 105 the proxy requires authentication in order to honour the request but did not accept any credentials offered (if any)
QNetworkReply::ContentAccessDenied 201 the access to the remote content was denied (similar to HTTP error 401)
QNetworkReply::ContentOperationNotPermittedError 202 the operation requested on the remote content is not permitted
QNetworkReply::ContentNotFoundError 203 the remote content was not found at the server (similar to HTTP error 404)
QNetworkReply::AuthenticationRequiredError 204 the remote server requires authentication to serve the content but the credentials provided were not accepted (if any)
QNetworkReply::ContentReSendError 205 the request needed to be sent again, but this failed for example because the upload data could not be read a second time.
QNetworkReply::ProtocolUnknownError 301 the Network Access API cannot honor the request because the protocol is not known
QNetworkReply::ProtocolInvalidOperationError 302 the requested operation is invalid for this protocol
QNetworkReply::UnknownNetworkError 99 an unknown network-related error was detected
QNetworkReply::UnknownProxyError 199 an unknown proxy-related error was detected
QNetworkReply::UnknownContentError 299 an unknown error related to the remote content was detected
QNetworkReply::ProtocolFailure 399 a breakdown in protocol was detected (parsing error, invalid or unexpected responses, etc.)
其中一些错误代码特定于 HTTP,但其他错误代码更通用。
这是一个完整的 QT FTP 客户端示例,以及文档。我建议在QFTP 类周围使用他们的包装器。
下载时处理错误的摘录:
if (ftp->currentCommand() == QFtp::Get) {
if (error) {
statusLabel->setText(tr("Canceled download of %1.")
.arg(file->fileName()));
file->close();
file->remove();
} else {
statusLabel->setText(tr("Downloaded %1 to current directory.")
.arg(file->fileName()));
file->close();
}
delete file;
enableDownloadButton();
progressDialog->hide();
这也是一个完整的演示。这是一个屏幕截图:
要在使用 QFtp 时处理网络异常,可以监听 stateChanged() 信号。如果状态变为 Closing 或 Unconnected,您可以检查 error() 是什么。
关于 QNAM 与 QFtp:QNAM 是更清洁和更新的 api,但两者都非常适合工作并得到官方支持。在 API 方面,QFtp 使用旧的命令 ID 模式(每个命令返回一个命令 ID),这要求我们跟踪命令(例如:找出发出信号的命令)。我发现 QNAM 的 api 模式要好得多,因为它的命令返回一个 QNetworkReply 对象,该对象反过来发出信号。但是,QNAM 的 api 似乎并没有针对 ftp 进行调整,也没有针对 http/s 进行处理(比如没有通过 ftp 删除文件),所以也许你现在最好坚持使用 QFtp。
正如我无法回答的评论中所述,QNetworkAccessManager 是用于满足常见需求而不是用于低级别访问的基本网络实用程序。
您可以执行的选项很少:
1) 自己实现 FTP 协议以及使用 QTcpSocket 和服务器所需的所有功能。
2)使用 QNetworkAccessManager 并希望您可以解决所有问题。
每种方法的好处应该很清楚,但请记住,Qt 不仅仅是用于创建 FTP 客户端的工具包。