{
Q_OBJECT
public:
~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:
bool m_httpRequestAborted;
int m_httpGetID;
};
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;
};
To copy to clipboard, switch view to plain text mode
NetWorkTest
::NetWorkTest(QWidget *parent
){
m_file
= new QFile("downloaded_file.txt");
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks