qt QNetworkAccessManager can not get any html
-
we use qt QNetworkAccessManager request a remote http url has problme,can not get any html,but can get the responese body size.this code:
manager=new QNetworkAccessManager(0); cookie = new QNetworkCookieJar(0); manager->setCookieJar(cookie); QString str_url="http://www.wangdaizhijia.com/daohang.html"; QNetworkRequest request; request.setUrl(QUrl(str_url)); request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); request.setRawHeader("Accept-Language", "zh-CN,zh;q=0.8"); request.setRawHeader("Cache-Control", "no-cache"); request.setRawHeader("Connection", "keep-alive"); request.setRawHeader("DNT","1"); request.setRawHeader("Pragma","no-cache"); request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"); QNetworkReply *reply=manager->get(request); QEventLoop eventLoop; QTimer timer; QObject::connect(reply, SIGNAL(finished()),&eventLoop, SLOT(quit())); QObject::connect(&timer, SIGNAL(timeout()),&eventLoop, SLOT(quit())); timer.start(5*1000); eventLoop.exec(); if(!timer.isActive()) { timer.stop(); } QByteArray temp=reply->readAll(); qDebug()<<"size is"<<temp.size(); qDebug()<<"html is"<<temp;
but if request other url e.g. http://www.google.com ,it's work,no problem,why? please help me,thanks.
Edited: Please put code after ``` (3 backticks) and end with the same - p3c0
-
@seamanmei The reply must be taking more time and by the time it comes your timer already kills the event loop. Try setting a greater timeout than current one. In case of google it works, as it first just returns a redirect info page which is usually fast.
AFAIK the recommended way would be not to use your own eventloop and just rely on theQNetworkAccessManager
's signals and your own slots. In this case you can just connectQNetworkAccessManager
'sfinished(QNetworkReply*)
signal to your own slot where there you can read the data usingQNetworkReply
'sreadAll()
. -
@p3c0 thank you answer my question,im try