QT单进程下载

QT    同步下载

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtCore>
#include <iostream>
using namespace std ;
class TT : public QObject
{
    Q_OBJECT
public:
    static QString getHtml(QString url)
    {
        QNetworkAccessManager *manager = new QNetworkAccessManager();
        QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
        QByteArray responseData;
        QEventLoop eventLoop;
        connect(manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
        eventLoop.exec();       //block until finish
        responseData = reply->readAll();
        return QString(responseData);
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString data = TT::getHtml(QString("http://outofmemory.cn );
    cout<<data.toStdString()<<endl;
    return a.exec();
}

 

上一篇:Apizza在线接口工具动态绑定API参数依赖


下一篇:JSONP不支持循环调用