#include "CPost.h"
CPost::CPost()
{
m_req = new QHttp;
connect(m_req, SIGNAL(dataReadProgress(int, int)), this, SLOT(dataReadProgress(int, int)));
connect(m_req, SIGNAL(dataSendProgress(int, int)), this, SLOT(dataSendProgress(int, int)));
connect(m_req, SIGNAL(done(bool)), this, SLOT(done(bool)));
connect(m_req, SIGNAL(requestFinished(int, bool)), this, SLOT(requestFinished(int, bool)));
connect(m_req, SIGNAL(requestStarted(int)), this, SLOT(requestStarted(int)));
connect(m_req, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(responseHeaderReceived(const QHttpResponseHeader &)));
connect(m_req, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
}
void CPost::launch()
{
m_fiReponse = new QFile("out.html");
m_fiReponse->open(QIODevice::WriteOnly | QIODevice::Text);
QByteArray bytes;
bytes.append("--AaB03x\r\n");
bytes.append("Content-Disposition: ");
bytes.append("form-data; name=\"uploaded_file\"; filename=\"test\"\r\n");
bytes.append("\r\n");
bytes.append("");
bytes.append("
");
bytes.append("Exemple XHTML 1.0");
bytes.append("");
bytes.append("");
bytes.append("");
bytes.append("- Tous les elements doivent etre explicitement balises.
");
bytes.append("- Les balises fermantes ne sont pas optionnelles.
");
bytes.append("
");
bytes.append("");
bytes.append("");
bytes.append("\r\n");
bytes.append("--AaB03x--");
QHttpRequestHeader header("POST", "/check");
header.setValue("Host", "validator.w3.org");
header.setContentType("multipart/form-data, boundary=AaB03x");
header.setValue("Cache-Control", "no-cache");
header.setValue("Accept","*/*");
header.setContentLength(bytes.length());
m_req->setHost("validator.w3.org", 80);
m_req->request(header, bytes, m_fiReponse);
}
void CPost::dataReadProgress(int done, int total)
{
qDebug() << "read:" << done << "/" << total;
}
void CPost::dataSendProgress(int done, int total)
{
qDebug() << "send:" << done << "/" << total;
}
void CPost::done(bool error)
{
qDebug() << "done" << error;
}
void CPost::requestFinished(int id, bool error)
{
qDebug() << "request finished:" << id << error;
m_fiReponse->close();
}
void CPost::requestStarted(int id)
{
qDebug() << "request started" << id;
}
void CPost::responseHeaderReceived(const QHttpResponseHeader &resp)
{
qDebug() << "---response header---";
qDebug() << resp.statusCode() << resp.reasonPhrase();
foreach(QString key, resp.keys())
qDebug() << key << resp.value(key);
qDebug() << "--- ---";
}
void CPost::stateChanged(int state)
{
qDebug() << "state:" << state;
}