Important: Please read the Qt Code of Conduct -

qt QNetworkAccessManager can not get any html

  • we use qt QNetworkAccessManager request a remote http url has problme,can not get any html,but can get the responese body size.this code:

       manager=new QNetworkAccessManager(0);
        cookie = new QNetworkCookieJar(0);
        QString str_url="";
        QNetworkRequest request;
        request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        request.setRawHeader("Accept-Language", "zh-CN,zh;q=0.8");
        request.setRawHeader("Cache-Control", "no-cache");
        request.setRawHeader("Connection", "keep-alive");
        request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36");
    QNetworkReply *reply=manager->get(request);
        QEventLoop eventLoop;
        QTimer timer;
        QObject::connect(reply, SIGNAL(finished()),&eventLoop, SLOT(quit()));
        QObject::connect(&timer, SIGNAL(timeout()),&eventLoop, SLOT(quit()));
        QByteArray temp=reply->readAll();
        qDebug()<<"size is"<<temp.size();
        qDebug()<<"html is"<<temp;

    but if request other url e.g. ,it's work,no problem,why? please help me,thanks.

    Edited: Please put code after ``` (3 backticks) and end with the same - p3c0

  • Moderators

    @seamanmei The reply must be taking more time and by the time it comes your timer already kills the event loop. Try setting a greater timeout than current one. In case of google it works, as it first just returns a redirect info page which is usually fast.
    AFAIK the recommended way would be not to use your own eventloop and just rely on the QNetworkAccessManager's signals and your own slots. In this case you can just connect QNetworkAccessManager's finished(QNetworkReply*) signal to your own slot where there you can read the data using QNetworkReply's readAll().

  • @p3c0 thank you answer my question,im try

Log in to reply