#include "httprequest.h"
#include<QDebug>
#include<QSslConfiguration>
#include<QSsl>
#include "terminalsetting.h"
const QString HttpRequest
::CONTENT_TYPE_APPLICATION_OCTET_STREAM="application/octet-stream";
//application/octet-stream const QString HttpRequest
::CONTENT_TYPE_APPLICATION_TEXT_PLAIN="text/plain";
const QString HttpRequest
::CONTENT_TYPE_IMAGE_PNG="image/png";
const QString HttpRequest
::CONTENT_TYPE_TAR="application/x-tar";
//const QString HttpRequest::CERTIFICATE_PATH="/mnt/jffs2/blb/cert/cert.crt";
{
//_http = new QNetworkAccessManager(this);
}
HttpRequest::~HttpRequest()
{
// delete _http;
}
void HttpRequest::initCertificates(){
qDebug()<<" == initCertificates == ";
QList<QSslCertificate> CaCert= QSslCertificate::fromPath(TerminalSetting::getSSLCertificateFolder().append("/cert.crt"));
QSslSocket::setDefaultCaCertificates(CaCert);
}
RestResponse HttpRequest
::get(const QString &requestUrl,
const long timeoutInSecond
){
QNetworkAccessManager mgr;
QObject::connect(&mgr,
SIGNAL(finished
(QNetworkReply
*)),
&eventLoop,
SLOT(quit
()));
long TIME_OUT_IN_SECONDS = timeoutInSecond;
if(timeoutInSecond <= 0)
TIME_OUT_IN_SECONDS = 3600;
QNetworkRequest request;
request.
setUrl(QUrl(requestUrl
));
initCertificates();
qDebug()<<" == inside HttpRequest get == "<<requestUrl;
QSslConfiguration config = request.sslConfiguration();
config.setProtocol(QSsl::TlsV1);
request.setSslConfiguration(config);
timer.setSingleShot(true);
connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
QNetworkReply *reply = mgr.get(request);
timer.start(TIME_OUT_IN_SECONDS * 1000);
eventLoop.exec(); // blocks stack until "finished()" has been called
if(timer.isActive()){
qDebug()<<"Timer running...";
timer.stop();
}
//time out
else{
qDebug()<<" TIME OUT";
return RestResponse::nullResponse();
}
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status == 0)
return RestResponse::nullResponse();
if (reply->error() == QNetworkReply::NoError)
res = reply->readAll();
else
res.append(reply->errorString());
delete reply;
return RestResponse(status,res);
}
RestResponse HttpRequest
::post(const QString &url,
const QList<FormParam>
&data,
const long timeoutInSecond
){
QNetworkAccessManager mgr;
QObject::connect(&mgr,
SIGNAL(finished
(QNetworkReply
*)),
&eventLoop,
SLOT(quit
()));
foreach (FormParam param, data) {
postData.
append("--"+QString(POST_MSG_BOUNDARY
)+"\r\n");
postData.append("Content-Disposition: form-data; name=""\""+param.getName()+"\"");
postData.append("\r\n");
postData.append("Content-Type: "+param.getContentType()+"\r\n\r\n");
qDebug()<<"Content data " <<param.getValue();
postData.append(param.getValue());
postData.append("\r\n");
}
postData.
append("--"+QString(POST_MSG_BOUNDARY
)+"\r\n");
// the HTTP request
QNetworkRequest req(serviceUrl);
req.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
req.
setRawHeader(QString("Content-Type").
toAscii(),
QString("multipart/form-data; boundary=" + QString(POST_MSG_BOUNDARY
)).
toAscii());
//SSL Configuration
initCertificates();
qDebug()<<" == inside httpRequest post == ";
QSslConfiguration config = req.sslConfiguration();
config.setProtocol(QSsl::TlsV1);
req.setSslConfiguration(config);
long TIME_OUT_IN_SECONDS = timeoutInSecond;
if(timeoutInSecond <= 0)
TIME_OUT_IN_SECONDS = 3600;
timer.setSingleShot(true);
connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
QNetworkReply *reply = mgr.post(req,postData);
timer.start(TIME_OUT_IN_SECONDS * 1000);
eventLoop.exec(); // blocks stack until "finished()" has been called
if(timer.isActive()){
qDebug()<<"Timer running...";
timer.stop();
}
//time out
else{
qDebug()<<" TIME OUT";
return RestResponse::nullResponse();
}
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug()<<" int status ::: "<<status;
if(status == 0)
return RestResponse::nullResponse();
if (reply->error() == QNetworkReply::NoError)
res = reply->readAll();
else
res.append(reply->errorString());
delete reply;
return RestResponse(status,res);
}
RestResponse HttpRequest
::postJSON(const QString &url,
const QMap<QString, QString>
&data,
const long timeoutInSecond
){
QNetworkAccessManager mgr;
QObject::connect(&mgr,
SIGNAL(finished
(QNetworkReply
*)),
&eventLoop,
SLOT(quit
()));
int index = 0;
foreach
(QString key, data.
keys()) { if(index > 0 && index != (data.size()))
postData.append(",");
postData.append("\"");
postData.append(key);
postData.append("\"");
postData.append(":");
postData.append("\"");
postData.append(data.value(key));
postData.append("\"");
index++;
}
postData.append("}");
// the HTTP request
QNetworkRequest req(serviceUrl);
//SSL Configuration
initCertificates();
qDebug()<<" == inside httpRequest postJSON == ";
QSslConfiguration config = req.sslConfiguration();
config.setProtocol(QSsl::TlsV1);
req.setSslConfiguration(config);
long TIME_OUT_IN_SECONDS = timeoutInSecond;
if(timeoutInSecond <= 0)
TIME_OUT_IN_SECONDS = 3600;
timer.setSingleShot(true);
connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
req.setRawHeader("User-Agent", "CC v0.1");
req.setRawHeader("X-Custom-User-Agent", "CC Client");
req.setRawHeader("Content-Type", "application/json");
req.setRawHeader("Content-Length", postDataSize);
req.setRawHeader("Accept", "application/json"); //Accept:application/json
qDebug()<<postData;
QNetworkReply *reply = mgr.post(req,postData);
timer.start(TIME_OUT_IN_SECONDS * 1000);
eventLoop.exec(); // blocks stack until "finished()" has been called
if(timer.isActive()){
qDebug()<<"Timer running...";
timer.stop();
}
//time out
else{
qDebug()<<" TIME OUT";
return RestResponse::nullResponse();
}
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status == 0)
return RestResponse::nullResponse();
if (reply->error() == QNetworkReply::NoError)
res = reply->readAll();
else
res.append(reply->errorString());
delete reply;
return RestResponse(status,res);
}
#include "httprequest.h"
#include<QDebug>
#include<QSslConfiguration>
#include<QSsl>
#include "terminalsetting.h"
const QString HttpRequest::CONTENT_TYPE_APPLICATION_OCTET_STREAM="application/octet-stream"; //application/octet-stream
const QString HttpRequest::CONTENT_TYPE_APPLICATION_TEXT_PLAIN="text/plain";
const QString HttpRequest::CONTENT_TYPE_IMAGE_PNG="image/png";
const QString HttpRequest::CONTENT_TYPE_TAR="application/x-tar";
//const QString HttpRequest::CERTIFICATE_PATH="/mnt/jffs2/blb/cert/cert.crt";
HttpRequest::HttpRequest(QObject *parent = 0) : QObject(parent)
{
//_http = new QNetworkAccessManager(this);
}
HttpRequest::~HttpRequest()
{
// delete _http;
}
void HttpRequest::initCertificates(){
qDebug()<<" == initCertificates == ";
QList<QSslCertificate> CaCert= QSslCertificate::fromPath(TerminalSetting::getSSLCertificateFolder().append("/cert.crt"));
QSslSocket::setDefaultCaCertificates(CaCert);
}
RestResponse HttpRequest::get(const QString &requestUrl,const long timeoutInSecond)
{
QEventLoop eventLoop;
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QByteArray res;
long TIME_OUT_IN_SECONDS = timeoutInSecond;
if(timeoutInSecond <= 0)
TIME_OUT_IN_SECONDS = 3600;
QNetworkRequest request;
request.setUrl(QUrl(requestUrl));
initCertificates();
qDebug()<<" == inside HttpRequest get == "<<requestUrl;
QSslConfiguration config = request.sslConfiguration();
config.setProtocol(QSsl::TlsV1);
request.setSslConfiguration(config);
QTimer timer;
timer.setSingleShot(true);
connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
QNetworkReply *reply = mgr.get(request);
timer.start(TIME_OUT_IN_SECONDS * 1000);
eventLoop.exec(); // blocks stack until "finished()" has been called
if(timer.isActive()){
qDebug()<<"Timer running...";
timer.stop();
}
//time out
else{
qDebug()<<" TIME OUT";
return RestResponse::nullResponse();
}
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status == 0)
return RestResponse::nullResponse();
if (reply->error() == QNetworkReply::NoError)
res = reply->readAll();
else
res.append(reply->errorString());
delete reply;
return RestResponse(status,res);
}
RestResponse HttpRequest::post(const QString &url,const QList<FormParam> &data,const long timeoutInSecond)
{
QEventLoop eventLoop;
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QByteArray res;
QByteArray postData;
foreach (FormParam param, data) {
postData.append("--"+QString(POST_MSG_BOUNDARY)+"\r\n");
postData.append("Content-Disposition: form-data; name=""\""+param.getName()+"\"");
postData.append("\r\n");
postData.append("Content-Type: "+param.getContentType()+"\r\n\r\n");
qDebug()<<"Content data " <<param.getValue();
postData.append(param.getValue());
postData.append("\r\n");
}
postData.append("--"+QString(POST_MSG_BOUNDARY)+"\r\n");
QUrl serviceUrl(url);
// the HTTP request
QNetworkRequest req(serviceUrl);
req.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
req.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + QString(POST_MSG_BOUNDARY)).toAscii());
//SSL Configuration
initCertificates();
qDebug()<<" == inside httpRequest post == ";
QSslConfiguration config = req.sslConfiguration();
config.setProtocol(QSsl::TlsV1);
req.setSslConfiguration(config);
QTimer timer;
long TIME_OUT_IN_SECONDS = timeoutInSecond;
if(timeoutInSecond <= 0)
TIME_OUT_IN_SECONDS = 3600;
timer.setSingleShot(true);
connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
QNetworkReply *reply = mgr.post(req,postData);
timer.start(TIME_OUT_IN_SECONDS * 1000);
eventLoop.exec(); // blocks stack until "finished()" has been called
if(timer.isActive()){
qDebug()<<"Timer running...";
timer.stop();
}
//time out
else{
qDebug()<<" TIME OUT";
return RestResponse::nullResponse();
}
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug()<<" int status ::: "<<status;
if(status == 0)
return RestResponse::nullResponse();
if (reply->error() == QNetworkReply::NoError)
res = reply->readAll();
else
res.append(reply->errorString());
delete reply;
return RestResponse(status,res);
}
RestResponse HttpRequest::postJSON(const QString &url, const QMap<QString, QString> &data,const long timeoutInSecond)
{
QEventLoop eventLoop;
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QByteArray res;
QByteArray postData ="{";
int index = 0;
foreach (QString key, data.keys()) {
if(index > 0 && index != (data.size()))
postData.append(",");
postData.append("\"");
postData.append(key);
postData.append("\"");
postData.append(":");
postData.append("\"");
postData.append(data.value(key));
postData.append("\"");
index++;
}
postData.append("}");
QByteArray postDataSize = QByteArray::number(postData.size());
QUrl serviceUrl(url);
// the HTTP request
QNetworkRequest req(serviceUrl);
//SSL Configuration
initCertificates();
qDebug()<<" == inside httpRequest postJSON == ";
QSslConfiguration config = req.sslConfiguration();
config.setProtocol(QSsl::TlsV1);
req.setSslConfiguration(config);
QTimer timer;
long TIME_OUT_IN_SECONDS = timeoutInSecond;
if(timeoutInSecond <= 0)
TIME_OUT_IN_SECONDS = 3600;
timer.setSingleShot(true);
connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
req.setRawHeader("User-Agent", "CC v0.1");
req.setRawHeader("X-Custom-User-Agent", "CC Client");
req.setRawHeader("Content-Type", "application/json");
req.setRawHeader("Content-Length", postDataSize);
req.setRawHeader("Accept", "application/json"); //Accept:application/json
qDebug()<<postData;
QNetworkReply *reply = mgr.post(req,postData);
timer.start(TIME_OUT_IN_SECONDS * 1000);
eventLoop.exec(); // blocks stack until "finished()" has been called
if(timer.isActive()){
qDebug()<<"Timer running...";
timer.stop();
}
//time out
else{
qDebug()<<" TIME OUT";
return RestResponse::nullResponse();
}
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status == 0)
return RestResponse::nullResponse();
if (reply->error() == QNetworkReply::NoError)
res = reply->readAll();
else
res.append(reply->errorString());
delete reply;
return RestResponse(status,res);
}
To copy to clipboard, switch view to plain text mode
Bookmarks