Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't download HTML of website

Can't download HTML of website

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 408 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.
  • E Offline
    E Offline
    El3ctroGh0st
    wrote on last edited by El3ctroGh0st
    #1

    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?)

    1 Reply Last reply
    0
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @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

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      4

      • Login

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