获取网页代码时出现乱码
先上程序:
#include <QtCore> #include <QtNetwork> //网页地址 const QString URLSTR = "http://www.csdn.net/"; //储存网页代码的文件 const QString FILE_NAME = "code.txt"; int main(int argc, char **argv) { QCoreApplication app(argc, argv); QUrl url(URLSTR); QNetworkAccessManager manager; QEventLoop loop; qDebug() << "Reading code form " << URLSTR; //发出请求 QNetworkReply *reply = manager.get(QNetworkRequest(url)); //请求结束并下载完成后,退出子事件循环 QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); //开启子事件循环 loop.exec(); //将读到的信息写入文件 QString code = reply->readAll(); QFile file(FILE_NAME); file.open(QIODevice::WriteOnly); QTextStream out(&file); out << code << endl; file.close(); qDebug() << "Finished, the code have written to " << FILE_NAME; return 0; }