6

QT Network を使用して FTPClient を実装しようとしています。

ダウンロード中にネットワークケーブルが抜かれた、インターネット接続が切れていないなどの例外的なケースにどのように対処できますか..

FTPClient がそのようなイベントについて知るにはどうすればよいですか?また、そのような種類の通知を利用できますか?

done(bool) 、 ommandFinished ( int id, bool error ) などのシグナルを使用しようとしましたが、どのようなシグナルも取得していません。

4

5 に答える 5

4

廃止された QFtp を使用しているようです。finished() および error() シグナルを備えた QNetworkReply (および QNetworkAccessManager) を使用する必要があります: QNetworkReply のドキュメント

于 2010-12-01T14:34:36.647 に答える
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 に固有のものもありますが、より一般的なものもあります。

于 2010-12-10T17:29:28.150 に答える
1

以下は、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();

これも完全に動作するデモです。スクリーンショットは次のとおりです。

代替テキスト

于 2010-12-11T06:23:07.623 に答える
1

QFtp の使用時にネットワーク例外を処理するには、stateChanged() シグナルをリッスンできます。状態が Closing または Unconnected になると、error() が何であるかを確認できます。

QNAM と QFtp について: QNAM はよりクリーンで新しい API ですが、どちらも機能するように意図されており、公式にサポートされています。API に関しては、QFtp は古いコマンド id パターン (各コマンドはコマンド id を返す) を使用するため、コマンドを追跡する必要があります (例: シグナルが発生したコマンドを把握するため)。QNAM の API パターンは、そのコマンドがシグナルを送信する QNetworkReply オブジェクトを返すため、はるかに優れていることがわかりました。しかし、QN​​AM の API は ftp に合わせて調整されていないようで、http/s を処理する ( ftp 経由でファイルを削除しないなど) ため、今のところ QFtp にこだわるのがよいでしょう。

于 2010-12-09T12:41:03.423 に答える
0

QNetworkAccessManagerは、私が答えることができないコメントで述べられているように、一般的なニーズのための基本的なネットワークユーティリティであり、低レベルのアクセスのためではありません。

実行できるオプションはいくつかあります。

1)FTPプロトコルを自分で実装し、QTcpSocketとサーバーを使用して必要なすべての機能を実装します。

2)QNetworkAccessManagerを使用して、QNetworkAccessManagerに関するすべての問題を回避できることを願っています。

それぞれのアプローチの利点は明らかですが、QtはFTPクライアントを作成するための単なるツールキットではないことを忘れないでください。

于 2010-12-09T13:08:57.557 に答える