Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Code copied from 'http' example has issues
Forum Updated to NodeBB v4.3 + New Features

Code copied from 'http' example has issues

Scheduled Pinned Locked Moved Unsolved Qt 6
8 Posts 4 Posters 524 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AI_Messiah
    wrote on last edited by
    #1

    When I copied the code from the example into my own project I found that
    #include <QtNetwork> has to be #include <QtNetwork/QtNetwork> and #include <QNetworkAccessManager> has to be #include <QtNetwork/QNetworkAccessManager> the 'connect' method in the http events wants to be derived from a class and I get an error in a file that I will not be ready to understand for ten years some 'meta object thing'

    the header is

    #ifndef GETHTML_H
    #define GETHTML_H
    #include <QProgressDialog>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QUrl>
    #include <memory>
    class gethtml
    {
    public:
        gethtml();
        QString htmlContent;
        QString errorText;
        QString user;
        QString password;
        bool haslod;
        void downloadFile(QString myurl);
        //QVector<QChar> binContent;
    private slots:
        void startRequest(const QUrl &requestedUrl);
        void httpFinished();
        void httpReadyRead();
        void sslErrors(const QList<QSslError> &errors);
        void slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator);
    private:
        QUrl url;
        QNetworkAccessManager qnam;
        QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply;
        bool httpRequestAborted = false;
    };
    
    #endif // GETHTML_H
    
    

    the source is

    
    #include "gethtml.h"
    
    
    
    
    #include <QtNetwork/QtNetwork>
    #include <QUrl>
    
    #include <algorithm>
    #include <memory>
    
    //#if QT_CONFIG(ssl)
    gethtml::gethtml(){
    QNetworkAccessManager::connect(&qnam, &QNetworkAccessManager::authenticationRequired,
            this, &gethtml::slotAuthenticationRequired);
    }
    void gethtml::startRequest(const QUrl &requestedUrl)
    {
        url = requestedUrl;
        httpRequestAborted = false;
    
        //! [qnam-download]
        reply.reset(qnam.get(QNetworkRequest(url)));
        //! [qnam-download]
        //! [connecting-reply-to-slots]
        connect(reply.get(), &QNetworkReply::finished, this, &gethtml::httpFinished);
        //! [networkreply-readyread-1]
        connect(reply.get(), &QIODevice::readyRead, this, &gethtml::httpReadyRead);
        //! [networkreply-readyread-1]
    #if QT_CONFIG(ssl)
        //! [sslerrors-1]
        connect(reply.get(), &QNetworkReply::sslErrors, this, &gethtml::sslErrors);
        //! [sslerrors-1]
    #endif
        //! [connecting-reply-to-slots]
    
    errorText = "";
    haslod = false;
    
    
    
    
    
    //    statusLabel->setText(tr("Downloading %1...").arg(url.toString()));
    }
    
    void gethtml::downloadFile(QString myurl)
    {
        const QString urlSpec = myurl.trimmed();
        if (urlSpec.isEmpty())
            return;
    
        const QUrl newUrl = QUrl::fromUserInput(urlSpec);
        if (!newUrl.isValid()) {
    
            return;
        }
    
    
    
    
    
        // schedule the request
        startRequest(newUrl);
    }
    
    
    
    
    
    void gethtml::httpFinished()
    {
    
    
        //! [networkreply-error-handling-1]
        QNetworkReply::NetworkError error = reply->error();
        const QString &errorString = reply->errorString();
        //! [networkreply-error-handling-1]
        reply.reset();
        //! [networkreply-error-handling-2]
        if (error != QNetworkReply::NoError) {
    
            // For "request aborted" we handle the label and button in cancelDownload()
            if (!httpRequestAborted) {
                errorText = reply->error();
    
            }
            return;
        }
        //! [networkreply-error-handling-2]
    
        //statusLabel->setText(tr("Downloaded %1 bytes to %2\nin\n%3"));
    
       // if (launchCheckBox->isChecked())
        //    QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absoluteFilePath()));
        //downloadButton->setEnabled(true);
        haslod = true;
    }
    
    //! [networkreply-readyread-2]
    void gethtml::httpReadyRead()
    {
        // This slot gets called every time the QNetworkReply has new data.
        // We read all of its new data and write it into the file.
        // That way we use less RAM than when reading it at the finished()
        // signal of the QNetworkReply
    
            htmlContent += reply->readAll();
    
    }
    //! [networkreply-readyread-2]
    
    
    
    //! [qnam-auth-required-2]
    void gethtml::slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator)
    {
       // QDialog authenticationDialog;
        //Ui::Dialog ui;
        //ui.setupUi(&authenticationDialog);
        //authenticationDialog.adjustSize();
        //ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm(), url.host()));
    
        // Did the URL have information? Fill the UI.
        // This is only relevant if the URL-supplied credentials were wrong
        //ui.userEdit->setText(url.userName());
        //ui.passwordEdit->setText(url.password());
    
        //if (authenticationDialog.exec() == QDialog::Accepted) {
            authenticator->setUser(user);
            authenticator->setPassword(password);
      //  }
    }
    //! [qnam-auth-required-2]
    
    //#if QT_CONFIG(ssl)
    //! [sslerrors-2]
    void gethtml::sslErrors(const QList<QSslError> &errors)
    {
        QString errorString = "";
        for (const QSslError &error : errors) {
            if (!errorString.isEmpty())
                errorString += '\n';
            errorString += error.errorString();
        }
        errorText = errorString;
    
            //reply->ignoreSslErrors();
    
    }
    //! [sslerrors-2]
    //#endif
    
    

    I hope someone can help me.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @AI_Messiah said in Code copied from 'http' example has issues:

      When I copied the code from the example into my own project I found that

      You forgot to add the QtNetwork module as described in the documentation and the example also for sure has this line in the .pro-file

      qmake: QT += network

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • A Offline
        A Offline
        AI_Messiah
        wrote on last edited by
        #3

        Actually I did insert this 'QT += network' in the project file

        Christian EhrlicherC 1 Reply Last reply
        0
        • A AI_Messiah

          Actually I did insert this 'QT += network' in the project file

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AI_Messiah Then the compiler and linker will also use the correct include paths and libs. Don't forget to rerun qmake and you modified the correct pro file.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • A AI_Messiah

            When I copied the code from the example into my own project I found that
            #include <QtNetwork> has to be #include <QtNetwork/QtNetwork> and #include <QNetworkAccessManager> has to be #include <QtNetwork/QNetworkAccessManager> the 'connect' method in the http events wants to be derived from a class and I get an error in a file that I will not be ready to understand for ten years some 'meta object thing'

            the header is

            #ifndef GETHTML_H
            #define GETHTML_H
            #include <QProgressDialog>
            #include <QtNetwork/QNetworkAccessManager>
            #include <QUrl>
            #include <memory>
            class gethtml
            {
            public:
                gethtml();
                QString htmlContent;
                QString errorText;
                QString user;
                QString password;
                bool haslod;
                void downloadFile(QString myurl);
                //QVector<QChar> binContent;
            private slots:
                void startRequest(const QUrl &requestedUrl);
                void httpFinished();
                void httpReadyRead();
                void sslErrors(const QList<QSslError> &errors);
                void slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator);
            private:
                QUrl url;
                QNetworkAccessManager qnam;
                QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply;
                bool httpRequestAborted = false;
            };
            
            #endif // GETHTML_H
            
            

            the source is

            
            #include "gethtml.h"
            
            
            
            
            #include <QtNetwork/QtNetwork>
            #include <QUrl>
            
            #include <algorithm>
            #include <memory>
            
            //#if QT_CONFIG(ssl)
            gethtml::gethtml(){
            QNetworkAccessManager::connect(&qnam, &QNetworkAccessManager::authenticationRequired,
                    this, &gethtml::slotAuthenticationRequired);
            }
            void gethtml::startRequest(const QUrl &requestedUrl)
            {
                url = requestedUrl;
                httpRequestAborted = false;
            
                //! [qnam-download]
                reply.reset(qnam.get(QNetworkRequest(url)));
                //! [qnam-download]
                //! [connecting-reply-to-slots]
                connect(reply.get(), &QNetworkReply::finished, this, &gethtml::httpFinished);
                //! [networkreply-readyread-1]
                connect(reply.get(), &QIODevice::readyRead, this, &gethtml::httpReadyRead);
                //! [networkreply-readyread-1]
            #if QT_CONFIG(ssl)
                //! [sslerrors-1]
                connect(reply.get(), &QNetworkReply::sslErrors, this, &gethtml::sslErrors);
                //! [sslerrors-1]
            #endif
                //! [connecting-reply-to-slots]
            
            errorText = "";
            haslod = false;
            
            
            
            
            
            //    statusLabel->setText(tr("Downloading %1...").arg(url.toString()));
            }
            
            void gethtml::downloadFile(QString myurl)
            {
                const QString urlSpec = myurl.trimmed();
                if (urlSpec.isEmpty())
                    return;
            
                const QUrl newUrl = QUrl::fromUserInput(urlSpec);
                if (!newUrl.isValid()) {
            
                    return;
                }
            
            
            
            
            
                // schedule the request
                startRequest(newUrl);
            }
            
            
            
            
            
            void gethtml::httpFinished()
            {
            
            
                //! [networkreply-error-handling-1]
                QNetworkReply::NetworkError error = reply->error();
                const QString &errorString = reply->errorString();
                //! [networkreply-error-handling-1]
                reply.reset();
                //! [networkreply-error-handling-2]
                if (error != QNetworkReply::NoError) {
            
                    // For "request aborted" we handle the label and button in cancelDownload()
                    if (!httpRequestAborted) {
                        errorText = reply->error();
            
                    }
                    return;
                }
                //! [networkreply-error-handling-2]
            
                //statusLabel->setText(tr("Downloaded %1 bytes to %2\nin\n%3"));
            
               // if (launchCheckBox->isChecked())
                //    QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absoluteFilePath()));
                //downloadButton->setEnabled(true);
                haslod = true;
            }
            
            //! [networkreply-readyread-2]
            void gethtml::httpReadyRead()
            {
                // This slot gets called every time the QNetworkReply has new data.
                // We read all of its new data and write it into the file.
                // That way we use less RAM than when reading it at the finished()
                // signal of the QNetworkReply
            
                    htmlContent += reply->readAll();
            
            }
            //! [networkreply-readyread-2]
            
            
            
            //! [qnam-auth-required-2]
            void gethtml::slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator)
            {
               // QDialog authenticationDialog;
                //Ui::Dialog ui;
                //ui.setupUi(&authenticationDialog);
                //authenticationDialog.adjustSize();
                //ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm(), url.host()));
            
                // Did the URL have information? Fill the UI.
                // This is only relevant if the URL-supplied credentials were wrong
                //ui.userEdit->setText(url.userName());
                //ui.passwordEdit->setText(url.password());
            
                //if (authenticationDialog.exec() == QDialog::Accepted) {
                    authenticator->setUser(user);
                    authenticator->setPassword(password);
              //  }
            }
            //! [qnam-auth-required-2]
            
            //#if QT_CONFIG(ssl)
            //! [sslerrors-2]
            void gethtml::sslErrors(const QList<QSslError> &errors)
            {
                QString errorString = "";
                for (const QSslError &error : errors) {
                    if (!errorString.isEmpty())
                        errorString += '\n';
                    errorString += error.errorString();
                }
                errorText = errorString;
            
                    //reply->ignoreSslErrors();
            
            }
            //! [sslerrors-2]
            //#endif
            
            

            I hope someone can help me.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @AI_Messiah said in Code copied from 'http' example has issues:

            and I get an error in a file that I will not be ready to understand for ten years some 'meta object thing'

            After you have followed @Christian-Ehrlicher's instructions to rebuild thoroughly, if you still get this error you should paste it rather than giving us your interpretation! :)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mr_Ada
              wrote on last edited by
              #6

              I was hoping to see a solution here. I have been working on the https example and for me authentication doesn't seem to work and I know I am accessing a site that needs a log in (MyFitnessPal). The slot never gets called.

              Christian EhrlicherC 1 Reply Last reply
              0
              • M Mr_Ada

                I was hoping to see a solution here. I have been working on the https example and for me authentication doesn't seem to work and I know I am accessing a site that needs a log in (MyFitnessPal). The slot never gets called.

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Mr_Ada Don't see what this has to do with your initial issue...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                M 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @Mr_Ada Don't see what this has to do with your initial issue...

                  M Offline
                  M Offline
                  Mr_Ada
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher Sorry for the late response. I just saw the notification today. I am trying to communicate with MyFitnessPal using HTTP example from QT and the authentication signal never gets triggered. I don't know if it is me or maybe the openssl package I have installed or am using. Seems to not work in Windows or Android. So basically I am saying I did all that was said here but I still cannot communicate with MyFitnessPal. Could be something I am just not missing. I know that MFP uses OAuth and so does Qt so maybe I am not using the right link?

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved