Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Downloading http page with several panes

Downloading http page with several panes

Scheduled Pinned Locked Moved Unsolved QtWebEngine
1 Posts 1 Posters 218 Views
  • 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.
  • J Offline
    J Offline
    jcga
    wrote on last edited by jcga
    #1

    Hi,
    I wrote a simple code based on the Download Manager example (https://code.qt.io/cgit/qt/qtbase.git/tree/examples/network/downloadmanager?h=6.3) to download html pages. But when I download a page with several panes, only one of the panes is downloaded, the other are missing. If, instead, I save the page on the disk (using "save as" command of the chrome browser), all panes are present.

    Here is the code (this is code I'm currently working on, so it is not very clean):

    ************************ file downloadmanger.h ************************
    #ifndef DOWNLOADMANAGER_H
    #define DOWNLOADMANAGER_H
    #include <QtCore>
    #include <QtNetwork>
    
    class DownloadManager: public QObject
    {
        Q_OBJECT
        QNetworkAccessManager manager;
        QVector<QNetworkReply *> currentDownloads;
    
    public:
        DownloadManager();
        QNetworkReply* doDownload(const QUrl &url);
        static QString saveFileName(const QUrl &url);
        bool saveToDisk(const QString &filename, QIODevice *data);
        static bool isHttpRedirect(QNetworkReply *reply);
    
    public slots:
        void downloadFinished(QNetworkReply *reply);
    };
    
    #endif // DOWNLOADMANAGER_H
    
    

    ******************** file downloadmanager.cpp *********************

    #include "downloadmanager.h"
    #include "../../../libraries/commons/debugtools.h"
    #include <QUrl>
    
    DownloadManager::DownloadManager()
    {
        connect(&manager, &QNetworkAccessManager::finished,
                this, &DownloadManager::downloadFinished);
    }
    
    QNetworkReply* DownloadManager::doDownload(const QUrl &url)
    {
        QNetworkRequest request(url);
        QNetworkReply *reply = manager.get(request);
    
        return reply;
    }
    
    QString DownloadManager::saveFileName(const QUrl &url)
    {
        QString path = url.path();
        QString basename = QFileInfo(path).fileName();
    
        if (basename.isEmpty())
            basename = "download";
    
        if (QFile::exists(basename)) {
            // already exists, don't overwrite
            int i = 0;
            basename += '.';
            while (QFile::exists(basename + QString::number(i)))
                ++i;
    
            basename += QString::number(i);
        }
    
        return basename;
    }
    
    bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
    {
        QFile file(filename);
        VERIFY( file.open(QIODevice::WriteOnly | QIODevice::Text) );
        file.write(data->readAll());
        file.close();
    
    
    
        return true;
    }
    
    bool DownloadManager::isHttpRedirect(QNetworkReply *reply)
    {
        int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        return statusCode == 301 || statusCode == 302 || statusCode == 303
               || statusCode == 305 || statusCode == 307 || statusCode == 308;
    }
    
    void DownloadManager::downloadFinished(QNetworkReply *reply)
    {
        QUrl url = reply->url();
        if (reply->error())
        {
            fprintf(stderr, "Download of %s failed: %s\n",
                    url.toEncoded().constData(),
                    qPrintable(reply->errorString()));
        } else
        {
            if (isHttpRedirect(reply)) {
                fputs("Request was redirected.\n", stderr);
            } else
            {
                QString filename = saveFileName(url);
                if (saveToDisk(filename, reply)) {
                    printf("Download of %s succeeded (saved to %s)\n",
                           url.toEncoded().constData(), qPrintable(filename));
                }
            }
        }
    
        currentDownloads.removeAll(reply);
        reply->deleteLater();
    
        if (currentDownloads.isEmpty()) {
            // all downloads finished
            QCoreApplication::instance()->quit();
        }
    }
    

    ******************** file main.cpp *******************************

    #include <QtCore>
    #include <QtNetwork>
    
    #include "downloadmanager.h"
    
    #include <cstdio>
    
    QT_BEGIN_NAMESPACE
    class QSslError;
    QT_END_NAMESPACE
    
    using namespace std;
    
    
    int main(int argc, char **argv)
    {
        QCoreApplication app(argc, argv);
    
        DownloadManager manager;
        QNetworkReply* r=manager.doDownload(QUrl("www.amazon.fr/s?k=hp+notebook"));
    
        app.exec();
    }
    

    Here is a website where this phenomenon occurs:
    https://www.amazon.fr/s?k=hp+notebook
    Download manager downloads only the left pane, but not the offers on the right pane.

    All suggestions are welcome.

    Best,
    Jean-Claude

    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