#include <QtCore/QString>
#include <QtDebug>
#include "MainWindow.h"
MainWindow::MainWindow()
{
setupUi(this);
http = new QHttp();
http->setObjectName("http");
connect(http, SIGNAL(stateChanged(int)), this, SLOT(on_http_stateChanged(int)));
connect(http, SIGNAL(dataReadProgress(int, int)), this, SLOT(on_http_dataReadProgress(int, int)));
connect(http, SIGNAL(dataSendProgress(int, int)), this, SLOT(on_http_dataSendProgress(int, int)));
connect(http, SIGNAL(done(bool)), this, SLOT(on_http_done(bool)));
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(on_http_requestFinished(int, bool)));
connect(http, SIGNAL(requestStarted(int)), this, SLOT(on_http_requestStarted(int)));
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(on_http_responseHeaderReceived(const QHttpResponseHeader &)));
/*
file = new QFile("/home/bjp/workspace/AutoVote/index.html");
file->open(QIODevice::WriteOnly);
*/
}
MainWindow::~MainWindow()
{
/*
file->close();
delete file;
*/
delete http;
}
void MainWindow::on_http_stateChanged(int stat)
{
switch (stat)
{
case QHttp::Unconnected : qDebug() << "Unconnected\n" ;
break;
case QHttp::HostLookup : qDebug() << "HostLookup\n" ;
break;
case QHttp::Connecting : qDebug() << "Connecting\n" ;
break;
case QHttp::Sending : qDebug() << "Sending\n" ;
break;
case QHttp::Reading : qDebug() << "Reading\n" ;
break;
case QHttp::Connected : qDebug() << "Connected\n" ;
break;
case QHttp::Closing : qDebug() << "Closing\n" ;
break;
}
}
void MainWindow::on_http_dataReadProgress(int done, int total)
{
qDebug() << "Downloaded " << done << " bytes " << " out of " << total << "\n";
}
void MainWindow::on_http_dataSendProgress(int done, int total)
{
qDebug() << "Sended " << done << " bytes " << " out of " << total << "\n";
}
void MainWindow::on_http_done(bool error)
{
if (error)
{
qDebug() << http->errorString() << "\n";
}
else
{
qDebug() << "Session finished successfully\n";
//buffer.close();
httptext = http->readAll();
qDebug() << "Received Size: " << httptext.count() << "\n";
QString strSource(httptext);
TextEditHttp->setPlainText(strSource);
}
}
void MainWindow::on_http_requestFinished(int id, bool error)
{
qDebug() << id <<" Request Finished\n";
if (error)
{
qDebug() << "with errors\n";
qDebug() << http->errorString() << "\n";
}
else
{
qDebug() << " successfully \n";
}
}
void MainWindow::on_http_requestStarted(int id)
{
qDebug() << "Request Started\n";
}
void MainWindow::on_http_responseHeaderReceived(const QHttpResponseHeader &resp)
{
qDebug() << "HTTP response header received \n";
}
void MainWindow::on_PushButtonPost_clicked()
{
QString data("name=你好中国&id=1234567890&addr=asdfghjkl");
//http.post("
http://localhost/biselaw/SalesReport.xml", data, &file);
/*
buffer.setData(httptext);
buffer.open(QIODevice::WriteOnly);
*/
qDebug() << "Post Data: " << data << "\n";
http->setHost("localhost");
//http->post("/biselaw/vote.php", data.toUtf8());
QHttpRequestHeader header("POST", "/biselaw/vote.php") ;
header.setValue("Host", "localhost") ;
header.setContentType("application/x-www-form-urlencoded");
http->request(header, data.toUtf8()) ;
//QString strBuffer(httptext);
//TextEditHttp->setHtml(strBuffer);
}