#include "synchttp.h"
int main(int argc, char *argv[])
{
SyncHTTP h("http://www.capeconsultancy.com");//this working good but not localhost
///prepare output buffer
h.syncGet("/",&getOutput );
qDebug()<<getOutput.data();
a.exec();
}
#include "synchttp.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SyncHTTP h("http://www.capeconsultancy.com");//this working good but not localhost
///prepare output buffer
QBuffer getOutput;
h.syncGet("/",&getOutput );
qDebug()<<getOutput.data();
a.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef SYNCHTTP_H
#define SYNCHTTP_H
#include <QHttp>
#include <QEventLoop>
#include <QBuffer>
class SyncHTTP
: public QHttp{
Q_OBJECT
public:
/// structors
:QHttp(parent
),requestID
(-1),status
(false){}
SyncHTTP
( const QString & hostName, quint16 port
= 80,
QObject * parent
= 0 ) :QHttp(hostName,port,parent
),requestID
(-1),status
(false){}
virtual ~SyncHTTP(){}
/// send GET request and wait until finished
{
///connect the requestFinished signal to our finished slot
connect(this,SIGNAL(requestFinished(int,bool)),SLOT(finished(int,bool)));
/// start the request and store the requestID
requestID = get(path,to);
/// block until the request is finished
loop.exec();
/// return the request status
return status;
}
/// send POST request and wait until finished
{
///connect the requestFinished signal to our finished slot
connect(this,SIGNAL(requestFinished(int,bool)),SLOT(finished(int,bool)));
/// start the request and store the requestID
requestID = post(path, data , to );
/// block until the request is finished
loop.exec();
/// return the request status
return status;
}
bool syncPost
( const QString & path,
const QByteArray
& data,
QIODevice * to
= 0 ) {
/// create io device from QByteArray
buffer.setData(data);
return syncPost(path,&buffer,to);
}
protected slots:
virtual void finished(int idx, bool err)
{
/// check to see if it's the request we made
if(idx!=requestID)
return;
/// set status of the request
status = !err;
/// end the loop
loop.exit();
}
private:
/// id of current request
int requestID;
/// error status of current request
bool status;
/// event loop used to block until request finished
};
#endif
#ifndef SYNCHTTP_H
#define SYNCHTTP_H
#include <QHttp>
#include <QEventLoop>
#include <QBuffer>
class SyncHTTP: public QHttp
{
Q_OBJECT
public:
/// structors
SyncHTTP( QObject * parent = 0 )
:QHttp(parent),requestID(-1),status(false){}
SyncHTTP( const QString & hostName, quint16 port = 80, QObject * parent = 0 )
:QHttp(hostName,port,parent),requestID(-1),status(false){}
virtual ~SyncHTTP(){}
/// send GET request and wait until finished
bool syncGet ( const QString & path, QIODevice * to = 0 )
{
///connect the requestFinished signal to our finished slot
connect(this,SIGNAL(requestFinished(int,bool)),SLOT(finished(int,bool)));
/// start the request and store the requestID
requestID = get(path,to);
/// block until the request is finished
loop.exec();
/// return the request status
return status;
}
/// send POST request and wait until finished
bool syncPost ( const QString & path, QIODevice * data, QIODevice * to = 0 )
{
///connect the requestFinished signal to our finished slot
connect(this,SIGNAL(requestFinished(int,bool)),SLOT(finished(int,bool)));
/// start the request and store the requestID
requestID = post(path, data , to );
/// block until the request is finished
loop.exec();
/// return the request status
return status;
}
bool syncPost ( const QString & path, const QByteArray& data, QIODevice * to = 0 )
{
/// create io device from QByteArray
QBuffer buffer;
buffer.setData(data);
return syncPost(path,&buffer,to);
}
protected slots:
virtual void finished(int idx, bool err)
{
/// check to see if it's the request we made
if(idx!=requestID)
return;
/// set status of the request
status = !err;
/// end the loop
loop.exit();
}
private:
/// id of current request
int requestID;
/// error status of current request
bool status;
/// event loop used to block until request finished
QEventLoop loop;
};
#endif
To copy to clipboard, switch view to plain text mode
Bookmarks