CodeQString NetWork::getWebSource(QUrl url)
{
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkReply *reply;
qDebug() << "Reading html code form " << url;
reply = manager.get(QNetworkRequest(url));
//请求结束并下载完成后,退出子事件循环
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
//开启子事件循环
loop.exec();
QByteArray codeContent = reply->readAll();
//将获取到的网页源码写入文件
//一定要注意编码问题,否则很容易出现乱码的
return QTextCodec::codecForHtml(codeContent)->toUnicode(codeContent);
}
CodeqDebug() << getWebSource(QUrl("http://www.baidu.com/"));