dineshkumar
3rd February 2011, 09:00
Hi, I am working with sync http request. The code what i get for that working correct on remote host(example:http://www.capeconsultancy.com). But it doesn't working on local host's(example:localhost). what was the problem? please help me!
main.cpp
#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();
}
synchttp.h
#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)),SLO T(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)),SLO T(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
Thanks in advance
main.cpp
#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();
}
synchttp.h
#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)),SLO T(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)),SLO T(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
Thanks in advance