QNetworkAccessManager delete when error occured?



  • I'm working on a class with basic ftp operation using QNetworkAccessManager, QNetworkRequest, ...
    Downloading files from a ftp server works fine. Downloading a none existing file to look how error handling works, I get an error 203 "content not found error". The signal from my reply object emit the finished signal. After this signal is emitted i delete the reply object in the slot connected to this signal.
    In the next step i will download an existing file and get always the error 201 "content access denied". All operations are working with a new reply object.

    C_FtpClient::C_FtpClient(QObject *parent) :
    QObject(parent)
    {
        replyError = 0;
        m_hostAddress = "10.1.1.2";
        manager = new QNetworkAccessManager(this);
    }
    
    QNetworkRequest C_FtpClient::ftpRequest(QString filename)
    {
        QUrl url;
        QNetworkRequest req;
        url.setUrl("ftp://" + m_hostAddress + "/" +filename);
        url.setPassword("password");
        url.setUserName("user");
        req.setUrl(url);
        return req;
    }
    
    void C_FtpClient::readFile()
    {
    QNetworkRequest myrequest = ftpRequest("help.txt");
    QNetworkReply *myreply = manager->get(myrequest);
    connect (myreply, SIGNAL(finished()), this, SLOT(slReadCompleted()));
    connect (myreply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slReplyError(QNetworkReply::NetworkError)));
    }
    
    void C_FtpClient::slReplyError(QNetworkReply::NetworkError error)
    {
        qDebug() << "reply error:" << error << reply->errorString();
    //    manager->deleteLater();
    //    manager = new QNetworkAccessManager(this);
    }
    
    void C_FtpClient::slReadCompleted()
    {
        QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
        qDebug() << reply->readAll();
        reply->deleteLater();
    }
    

    Uncomment the block in function slReplyError will fix this problem, but is this the right way to handle errors deleting the QNetworkAccessManager and creating a new one?



  • your code works on my machine and ftp withour errors.
    I just use only one "reply" as class member



  • Yes it works for me also.
    But if I intentionally generating an error by trying to download a nonexistent file I get an error as expected. Next, I try again to download a existing file and always get an error even though the file exists.

    The only solution that I have for this behavior is to delete the manager class and recreate it.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How are you triggering the new download that fails ?



  • Hello SGaist,

    i use the same function readFile().

    • First, the file does not exist on the FTP server
    • Then I push a button to call the readFile() function
    • ---- I get an error 203 ----
    • Next i create help.txt on the FTP server
    • I push the button again to call readFile() function
    • ---- I get an error 201 ----

  • Lifetime Qt Champion

    Then there something else. 201 is not an error, it means "Created"

    Note that your slReplyError function doesn't seem to use the reply generated by your manager (myreply in readFile and reply in slReplyError).



  • I think that's not right.

    *myreply gets the reply object from my manager
    I set an object name for that reply object to check if it is the object what i mean.

    slReplyError casts the sender object and outputs the object name from the reply object that i am getting from the manager.
    The line where i do the cast i forgot in my first post. SORRY!!!

    void C_FtpClient::readFile()
    {
    qDebug() << Q_FUNC_INFO;
    QNetworkRequest myrequest = ftpRequest("help.txt");
    QNetworkReply *myreply = manager->get(myrequest);
    myreply->setObjectName("objMyReply");
    
    connect (myreply, SIGNAL(finished()), this, SLOT(slReadCompleted()));
    connect (myreply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slReplyError(QNetworkReply::NetworkError)));
    }
    
    void C_FtpClient::slReplyError(QNetworkReply::NetworkError error)
    {
    qDebug() << Q_FUNC_INFO;
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    qDebug() << "reply object name:" << reply->objectName();
    qDebug() << "reply error:" << error << reply->error();
    }
    

    thats the output

    Debug: void C_FtpClient::readFile()
    Debug: void C_FtpClient::slReplyError(QNetworkReply::NetworkError)
    Debug: reply object name: "objMyReply"
    Debug: reply error: 203 203
    Debug: reply object "objMyReply" will be killed later ;)
    Debug: ""

    Debug: void C_FtpClient::readFile()
    Debug: void C_FtpClient::slReplyError(QNetworkReply::NetworkError)
    Debug: reply object name: "objMyReply"
    Debug: reply error: 201 201
    Debug: reply object "objMyReply" will be killed later ;)
    Debug: ""

    and this is the error description i found in the documentation

    QNetworkReply::ContentAccessDenied 201 the access to the remote content was denied (similar to HTTP error 401)
    QNetworkReply::ContentNotFoundError 203 the remote content was not found at the server (similar to HTTP error 404)


  • Lifetime Qt Champion

    My bad, I've mixed the http status code with the NetworkError enum.

    Since you get a 201, did you check the permissions of the file you created ?



  • i am doing this

    -rw-r--r-- 1 root root 6 Nov 26 12:26 help.txt

    $ chown WEBUSER:WEBUSER help.txt
    -rw-r--r-- 1 WEBUSER WEBUSER 6 Nov 26 12:26 help.txt

    $ chmod ugo+xw help.txt
    -rwxrwxrwx 1 WEBUSER WEBUSER 6 Nov 26 12:26 help.txt

    always the same



  • Without doing anything. After time x it works without recreating the
    QNetworkAccessManager. I added a timestamp

    A timeout of about 3 minutes is not practical for me. I don't know why it is so, so my solution recreating the QNetworkAccessManager is for now the best approach.

    Debug: void C_FtpClient::readLogbook() QDateTime("Do. Nov 26 12:27:03 2015")
    Debug: void C_FtpClient::slReplyError(QNetworkReply::NetworkError)
    Debug: reply object name: "objMyReply"
    Debug: reply error: 203 203
    Debug: FTP DOWNLOAD: 0 from 0 sent
    Debug: reply object "objMyReply" will be killed later ;)
    Debug: ""

    file created on the server

    Debug: void C_FtpClient::readLogbook() QDateTime("Do. Nov 26 12:27:40 2015")
    Debug: void C_FtpClient::slReplyError(QNetworkReply::NetworkError)
    Debug: reply object name: "objMyReply"
    Debug: reply error: 201 201
    Debug: FTP DOWNLOAD: 0 from 0 sent
    Debug: reply object "objMyReply" will be killed later ;)
    Debug: ""

    Debug: void C_FtpClient::readLogbook() QDateTime("Do. Nov 26 12:31:39 2015")
    Debug: FTP DOWNLOAD: 6 from 6 sent
    Debug: reply object "objMyReply" will be killed later ;)
    Debug: "hello"


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.