Method to reset the QNetworkAccessManager backend

风流意气都作罢 提交于 2019-12-07 07:03:00

问题


It seems that QNetworkAccessManager does not handle missing files retrieved by ftp if the ftp server requires authentication.

The situation is this: I'm downloading multiple files from the same ftp server that requires username and password. I successfully download a few files then send a GET for a file that does not exist. That request reports failure. I then send a GET request for a file that should be valid. That request never emits a finished signal or error.

Qt 4.7.4

Please help! This is driving me nuts. I think that if I can somehow reset the ftp backend, this problem might be solvable.


回答1:


You are connecting the QNetworkReply signal finished() like this:

QObject::connect(reply, SIGNAL(finished()), netty, SLOT(finished()));

Instead try the finished signal from the QNetworkAccessManager like this:

connect(&network, SIGNAL(finished(QNetworkReply *)), netty, SLOT(finished(QNetworkReply *)));

Note that you will have to change the prototype for your finished() slot.

A final method would be to set a timer that calls a function like this:

void check(QNetworkReply *reply){
 if(reply != NULL){
   if(reply->isFinished())
     finished(reply);
   else
     reply->abort();
 }
}

And be aware of this bug:

https://bugreports.qt-project.org/browse/QTBUG-3443




回答2:


I'm experiencing the same situation with Qt 5.3.0 (okay its still beta but I strongly believe that the same bug is reproduced on Qt5.2.x).

QNetworkAccessManager correctly reports the fileNotFound error for ftp but does never recover from it. Any subsequent ftp requests fail with the same error message.

Suppose try to fetch badFile.txt (which does not exist). We get the correct message that badFile.txt was not found. If we then try to access goodFile.txt (which exists) we get

Error while downloading ftp://ftp.url.com/goodFile.txt: 
Downloading file failed:
Can't open /badFile.txt: No such file or directory"

The previous error seems to stay stuck in QNetworkAccessManager for ever.

The only solution that worked for me was to delete and create a new QNetworkAccessManager every time a ftp not found error occurs. This is done by simply connecting to the QNetworkReply error(QNetworkReply::NetworkError) signal. As stated by elmigranto, the finished signal will be emitted for each active request. This can obviously only work if the QNetworkAccessManager handles a single request, otherwise special care must be taken not to abort any parallel requests handled by the same manager.

The QNetworkAccessManager finished(QNetworkReply*) and the QNetworkReply finished() signals do exactly the same thing. As stated in the Qt docs these two signals are emitted in tandem. This is NOT a solution for this problem as stated by buster.

Setting up a timer can be useful for other purposes (such as connection timeout) but will not resolve this particular problem either.



来源:https://stackoverflow.com/questions/11587827/method-to-reset-the-qnetworkaccessmanager-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!