readyread不会被发出

时间:2018-03-14 07:51:50

标签: c++ qt qnetworkreply

我正在尝试在QT上获取网页。我想对一个类进行这个抓取操作。但是,不会发出startRequest函数中的readyread信号。我无法获取任何数据。此代码创建HTML文件,但不会向其中写入任何内容。

#include "fetchoperation.h"
#include <QtWidgets>
#include <QtNetwork>
#include <QUrl>
#include <QTextBrowser>
#include <iostream>    
using namespace std;
fetchoperation::fetchoperation()
{

}
void fetchoperation::downloadFile(QUrl geturl,QString th)
{
    page=th;
    qnam = new QNetworkAccessManager();
    url=geturl;
    QFileInfo fileInfo(url.path());
    QString fileName = fileInfo.fileName();
    fileName = "C:/yunus/"+page+".html";
    file = new QFile(fileName);
    file->open(QIODevice::WriteOnly);
    startRequest(url);


}
void fetchoperation::httpReadyRead()
{
    if (file)
        file->write(reply->readAll());
}
void fetchoperation::httpDownloadFinished()
{   
    file->flush();
    file->close();

    QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
             QString fileName = QFileInfo(QUrl(url).path()).fileName();

         reply->deleteLater();
         reply = 0;
         delete file;
         file = 0;
        QDesktopServices::openUrl(QUrl::fromLocalFile("C:/yunus/"+page+".html"));
}
void fetchoperation::startRequest(QUrl url)
{   

    reply = qnam->get(QNetworkRequest(url));

    connect(reply,&QNetworkReply::readyRead,
            this,&fetchoperation::httpReadyRead);

    connect(reply,&QNetworkReply::finished,
            this,&fetchoperation::httpDownloadFinished);
}

0 个答案:

没有答案