PDA

View Full Version : QtNetwork and QHttp->Problem with reading the response header



aMan
7th September 2006, 23:53
Hi..
I have a problem related to a get request in QHttp. I never programmed anything that has to do with network (only PHP and HTML, but that doesn't count), so I'm completely new on this topic..

I'm trying to send a get request to the local apacha and then read the response code. The server gets the request and the html file gets saved to downloaded_file.txt. So that's working.
But I want to know the status code of the server, and that's not working.

Here is the code:

header file

class NetWorkTest : public QWidget
{
Q_OBJECT
public:
NetWorkTest(QWidget *parent = 0);
~NetWorkTest();

protected slots:
void sendHttpRequest();
void act_requestStarted(int p_requestId);
void act_requestFinished(int p_requestId, bool p_er);
void act_readResponseHeader(const QHttpResponseHeader& p_resp);

protected:
QLayout* m_layout;
QHttp* m_http;
QFile* m_file;

bool m_httpRequestAborted;
int m_httpGetID;
};


cpp file

NetWorkTest::NetWorkTest(QWidget *parent)
: QWidget(parent)
{
m_file = new QFile("downloaded_file.txt");
m_file->open(QIODevice::WriteOnly);

m_http = new QHttp(this);

connect(button, SIGNAL(clicked()), this, SLOT(sendHttpRequest()));
connect(m_http, SIGNAL(requestStarted(int)), this, SLOT(act_requestStarted(int)));
connect(m_http, SIGNAL(requestFinished(int, bool)), this, SLOT(act_requestFinished(int, bool)));
connect(m_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(act_requestFinished(const QHttpResponseHeader&)));

m_http->setHost("127.0.0.1", 8000);
}

void NetWorkTest::sendHttpRequest()
{
m_httpGetID = m_http->get("/test.html", m_file);
cout << "Requesting " << m_httpGetID << endl;
}

void NetWorkTest::act_requestStarted(int p_requestId)
{
cout << "Request " << p_requestId << " started." << endl;
}

void NetWorkTest::act_requestFinished(int p_requestId, bool p_er)
{
cout << "Request " << p_requestId << " finished";
if (p_er) cout << " with an error. (" << m_http->errorString().toStdString() << ")" << endl;
else cout << "." << endl;
}

void NetWorkTest::act_readResponseHeader(const QHttpResponseHeader& p_resp) // never got this signal..
{
if(p_resp.statusCode() != 200) {
cout << "Server error " << p_resp.statusCode() << " (" << p_resp.reasonPhrase().toStdString() << ")" << endl;
}
else cout << "All good.." << endl;
m_http->abort();
}

I've also appended the source, in case you want something to compile..

Regards..
aMan..

danadam
8th September 2006, 00:58
In your code responseHeaderReceived() signal is connected to act_requestFinished() slot, not to act_readResponseHeader() slot. act_readResponseHeader() is never called.

aMan
8th September 2006, 11:35
God, such a stupid mistake..
I've checked the parameter of the response slot a hundred times, but never the name..

But it works now..
Thank you..

aMan..