Important: Please read the Qt Code of Conduct -

Can't download HTML of website

  • Hey,

    so this is the source code of my class that should download the HTML of a specific web page:

    #ifndef XMLPARSER_HPP
    #define XMLPARSER_HPP
    #include <QNetworkReply>
    #include <QObject>
    #include <QUrl>
    class XMLParser : public QObject
        explicit XMLParser(QObject *parent = 0) {}
        XMLParser(QUrl link, QObject *parent = 0) : downloadLink(link) {}
        QString getContent() const { return downloadData; }
        void downloadXML();
    public slots:
        void readContent(QNetworkReply *);
        QUrl downloadLink;
        QString downloadData;
    #endif // XMLPARSER_HPP
    #include "xmlparser.hpp"
    #include <QDebug>
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    void XMLParser::downloadXML()
        QNetworkRequest qreq(downloadLink);
        QNetworkAccessManager *qmanag = new QNetworkAccessManager(this);
        connect(qmanag, &QNetworkAccessManager::finished, this, XMLParser::readContent);
    void XMLParser::readContent(QNetworkReply *qrep)
        QByteArray content;
        content = qrep->readAll();
        downloadData = QString(content);
        if(qrep->error() == QNetworkReply::NoError)
            qDebug() << "No error";
        qDebug() << downloadData;

    So far so good. In another class I call the following:

        XMLParser *pars = new XMLParser(QUrl(""), this);

    When I run this the program prints "No error", which leads me to believe that network-wise everything is working. However, it seems that downloadData is completely empty because it doesn't print anything at all. Anyone knows what I might've done wrong?
    EDIT: Actually no, it seems that the problem is just that I can't print such large strings into QDebug. Because if I print downloadData[0] it works, However, I'll still keep it open in the hope that someone can help me with the issue below.

    (On a sidenote. I keep getting the warning that parent is an unused parameter. Is that something I can ignore or did I do something wrong in the way I configured the constructors?)

  • @El3ctroGh0st three things here.

    1. What happens with my network-based application? Wireshark is always your friend to know exactly what your application is sending/receiving from/to the network.
    2. Regarding parent is an unused parameter, try using the parent you receive in the constructor into the QObject constructor:
    XMLParser(QUrl link, QObject *parent = 0) : QObject(parent), downloadLink(link) {}
    1. Try not to hijaack the topic of your post with several subjects inside it :-)
      You started with the network issue, but you have the parent issue as well

Log in to reply