A little new to Qt and trying to make an HTTP GET request to a website. The problem is it doesn't send anything. I've checked in fiddler. There is no movement. There are no errors, the signals aren't fired up.
I've also checked if I have any other network interfaces that might be used so I disabled them and that didn't help
Here is the source:
linkslooker::linkslooker(QThread *thread){
this->mainThread = thread;
Loop();
}
void linkslooker::Loop(){
// while(true){
qDebug() << "Looking for sites. Accessability:" << qnam.networkAccessible(); // Always 1
mainThread->msleep(1000); //To slow down the loop
QNetworkRequest req(QUrl("http://www.test.com"));
reply = qnam.get(req); //Get request to a QNetworkReply
if(reply->error() != QNetworkReply::NoError)
qDebug("network error!"); //There are no errors
connect(reply, SIGNAL(finished()),
SLOT(httpFinished())); //Finish request signal
connect(reply, SIGNAL(readyRead()),
this, SLOT(httpReadyRead())); //Read to start signal
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(updateDataReadProgress(qint64,qint64))); //Reading Signal
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError))); //Error Signal
// }
}
void linkslooker::httpFinished(){
qDebug() << QString::fromUtf8(m_data);
}
void linkslooker::slotError(QNetworkReply::NetworkError e) {
qDebug() << "slotError" << e ; //Not fired up
}
void linkslooker::httpReadyRead()
{
m_data.append(reply->readAll());
qDebug() << "READY TO READ";
}
void linkslooker::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
if (httpRequestAborted)
qDebug() << "Network request Aborted";
qDebug() << "Have read " << bytesRead << "Out of " << totalBytes;
}
Here is the header for reference:
#ifndef LINKSLOOKER_H
#define LINKSLOOKER_H
#include <QCoreApplication>
#include <QQueue>
#include <QtNetwork>
#include <QList>
#include <QNetworkAccessManager>
class linkslooker:public QObject{
Q_OBJECT
public:
linkslooker(QThread *);
~linkslooker();
private:
void Loop();
QThread *mainThread;
QQueue<QString> websites;
QList<QString> searched;
QNetworkAccessManager qnam;
QNetworkReply *reply;
bool httpRequestAborted;
QByteArray m_data;
private slots:
void httpFinished();
void httpReadyRead();
void updateDataReadProgress(qint64 bytesRead, qint64 totalBytes);
void slotError(QNetworkReply::NetworkError);
};
#endif // LINKSLOOKER_H
Found the problem:
While calling this class I have not given it a variable to be held in, so after it attached all the signals - the class was destroyed (because it was out of scope I assume?)
Had to change from this:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
linkslooker(a.thread());
return a.exec();
}
To this:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
linkslooker ll(a.thread());
return a.exec();
}