2019独角兽企业重金招聘Python工程师标准>>>
#include <QNetworkRequest>//包含QNetworkRequest类
#include <QNetworkReply>//包含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://www.baidu.com") );//打开百度
cout<<data.toStdString()<<endl;//输出百度返回的内容
return a.exec();
}