Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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:

    //xmlparser.hpp
    #ifndef XMLPARSER_HPP
    #define XMLPARSER_HPP
    
    #include <QNetworkReply>
    #include <QObject>
    #include <QUrl>
    
    class XMLParser : public QObject
    {
        Q_OBJECT
    public:
        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 *);
    
    private:
        QUrl downloadLink;
        QString downloadData;
    };
    
    #endif // XMLPARSER_HPP
    
    //xmlparser.cpp
    #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);
    
        qmanag->get(qreq);
    }
    
    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("https://forum.qt.io/category/10/general-and-desktop"), this);
        pars->downloadXML();
    

    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