I'm trying to check if websites repond to me. So, I have a list and I'm sequentially testing them with QNetworkAccessManager like shown below. The problem is that although I'm trying, the process may still be a bit asyncroneous, meaning that the replyFinished() function is not called without a lag or too late or while the next URL is already tested. I know this because I sometimes get wrong results. Does anyone have ideas, I'd be very thankful.
This is my main function:
QUrl url; QNetworkAccessManager manager;
for(int y=1; y<100+1; y++){
tT.setSingleShot(true);
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
connect(&manager, SIGNAL(finished(QNetworkReply*)), &q, SLOT(quit()));
QNetworkReply *reply = manager.get(QNetworkRequest(url));//this URL is different in every for cycle
tT.start(5000); // 5s timeout
q.exec();
if(tT.isActive()){
// download complete
tT.stop();
replyFinished(reply);
} else {
// timeout
}
}
}
QUrl url; QNetworkAccessManager manager;
QEventLoop q; QTimer tT;
for(int y=1; y<100+1; y++){
tT.setSingleShot(true);
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
connect(&manager, SIGNAL(finished(QNetworkReply*)), &q, SLOT(quit()));
QNetworkReply *reply = manager.get(QNetworkRequest(url));//this URL is different in every for cycle
tT.start(5000); // 5s timeout
q.exec();
if(tT.isActive()){
// download complete
tT.stop();
replyFinished(reply);
} else {
// timeout
}
QApplication::processEvents();
}
QApplication::processEvents();
}
To copy to clipboard, switch view to plain text mode
This is my replyFinished() slot:
void MyClass::replyFinished(QNetworkReply* reply)
{
QVariant statusCodeV
= reply
->attribute
(QNetworkRequest
::HttpStatusCodeAttribute);
if (statusCodeV.toInt()!=0){
//report that website reponded
}
else{
//report that website didn't respond
}
delete reply;
}
void MyClass::replyFinished(QNetworkReply* reply)
{
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (statusCodeV.toInt()!=0){
//report that website reponded
}
else{
//report that website didn't respond
}
delete reply;
}
To copy to clipboard, switch view to plain text mode
Thnaks!
Bookmarks