我正在尝试在线获取网页的 HTML 源代码,我的代码如下:
void Helper::start()
{
QString url = "http://www.youtube-mp3.org/get?video_id=PnL4Z0ebcBc";
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0;)");
QNetworkReply *reply = manager->get(request);
QObject::connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
}
void Helper::onFinished()
{
QIODevice * content = static_cast<QIODevice*>(QObject::sender());
QString html = content->readAll();
content->deleteLater();
qDebug() << html; // It's empty!!
}
void Helper::onError(QNetworkReply::NetworkError err)
{
qDebug() << "Starting On Error ....";
QIODevice * content = static_cast<QIODevice*>(QObject::sender());
QString error = content->errorString();
content->deleteLater();
qDebug() << error;
}
字符串是空的html
,我不知道为什么!
当我将 URL 更改为 Google 的时,它可以工作,有什么问题吗?(因为我没有收到任何错误)