我正在尝试使用QT网络实现FTPClient。
我如何处理例如下载网络电缆时的特殊情况,而不是网络连接等等。?
我的FTPClient如何才能了解此类事件,是否有这样的通知?
我试图使用像done(bool),ommandFinished(int id,bool error)这样的信号,但我没有得到任何信号。
答案 0 :(得分:4)
你似乎使用的QFtp已经过时了。您应该使用QNetworkReply(和QNetworkAccessManager),它已经完成了()和error()信号: QNetworkReply documentation。
答案 1 :(得分:3)
您是否尝试过创建自定义SLOT并将其连接到QNetworkReply 错误 SIGNAL?
然后,您可以检查参数以确定错误并确定您希望如何处理它。
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,但其他错误代码更通用。
答案 2 :(得分:1)
要在使用QFtp时处理网络异常,您可以侦听stateChanged()信号。如果状态变为Closing或Unconnected,则可以检查error()是什么。
关于QNAM与QFtp:QNAM是更清洁和更新的api,但两者都非常适合工作和官方支持。在API方面,QFtp使用旧的命令id模式(每个命令返回一个命令id),这需要我们跟踪命令(例如:找出信号被引发的命令)。我发现QNAM的api模式要好得多,因为它的命令返回一个QNetworkReply对象,后者又发出信号。但是,QNAM的api似乎没有调整ftp以及它处理http / s(如no deletion of files over ftp),所以也许你现在很好地坚持QFtp。
答案 3 :(得分:1)
这是complete example of an QT FTP client以及文档。我建议在QFTP class周围使用他们的包装。
下载时处理错误的摘录:
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();
这也是一个完全有效的演示。这是一个截图:
答案 4 :(得分:0)
QNetworkAccessManager,如我无法回答的评论中所述,是基本网络实用程序,用于满足常见需求,而不是低级别访问。
您可以做的选择很少:
1)使用QTcpSocket和服务器自己实现FTP协议以及所需的所有功能。
2)使用QNetworkAccessManager并希望您可以解决它的所有问题。
每种方法的好处都应该清楚,但请记住,Qt不仅仅是FTP客户端创建的工具包。