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(); }