Many Thanks for replying. It's right I've not helped you with more details.
In fact, my application which should connect to the server is
void Connexion
::connectToWebService(QString host, quint16 port,
QString path, vector <QStringList> paramsList
) {
// formater la chaine à envoyer
int i = 0;
int listParamSize = paramsList.size()-1;
while(i <= listParamSize){
if(0 < i){
params += "&";
}
params
+= QString(paramsList
[i
].
at(0).
toLocal8Bit().
constData())+"="+QUrl::toPercentEncoding(QString(paramsList
[i
].
at(1).
toLocal8Bit().
constData()));
++i;
}
//invoquer le service web
this->http.clearPendingRequests();
header.setValue("HOST", host);
header.setValue("Cache-Control", "no-cache");
header.setContentType("application/x-www-form-urlencoded");
header.setContentLength(params.length());
this->http.setHost(host);
this->idVerify = this->http.request(header, params.toUtf8());
this->http.closeConnection();
void Connexion::connectToWebService(QString host, quint16 port, QString path, vector <QStringList> paramsList)
{
// formater la chaine à envoyer
QString params;
int i = 0;
int listParamSize = paramsList.size()-1;
while(i <= listParamSize){
if(0 < i){
params += "&";
}
params += QString(paramsList[i].at(0).toLocal8Bit().constData())+"="+QUrl::toPercentEncoding(QString(paramsList[i].at(1).toLocal8Bit().constData()));
++i;
}
//invoquer le service web
this->http.clearPendingRequests();
QHttpRequestHeader header("POST", path);
header.setValue("HOST", host);
header.setValue("Cache-Control", "no-cache");
header.setContentType("application/x-www-form-urlencoded");
header.setContentLength(params.length());
this->http.setHost(host);
this->idVerify = this->http.request(header, params.toUtf8());
this->http.closeConnection();
To copy to clipboard, switch view to plain text mode
The http is not declared as QHttp http; in .h file (not as pointer) and I've connected it like
connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(receive(int, bool)));
connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(receive(int, bool)));
To copy to clipboard, switch view to plain text mode
After that, I've implemented the functionc receive witch connected to the requestFinished slot like
void Connexion::receive(int id, bool error)
{
if(error)
{
qDebug() << http.errorString();
this->result = http.errorString();
}
else
{
if(id == this->idVerify )
{
QString res
= textCodec
->toUnicode
(this
->http.
readAll());
doc.setContent(res);
result = docElem.text();
// fonction à appeler après reception données
startApplication();
}
}
}
void Connexion::receive(int id, bool error)
{
if(error)
{
qDebug() << http.errorString();
this->result = http.errorString();
}
else
{
if(id == this->idVerify )
{
QTextCodec *textCodec = QTextCodec::codecForName("utf-8");
QString res = textCodec->toUnicode(this->http.readAll());
QDomDocument doc;
doc.setContent(res);
QDomElement docElem = doc.documentElement();
result = docElem.text();
// fonction à appeler après reception données
startApplication();
}
}
}
To copy to clipboard, switch view to plain text mode
Note that in the receive function, I call a function called startApplication and in last one I show a messageBox if the WebService returned an error.
But after displying the messageBox, I wait some time and the my application access to the receive function another time and a second messageBox is shown. If I click Ok on the 2 messages the application crashes.
I've tried some solutions but everytime it does not work :
1. Use http like a pointer and delete it if a have an error but it does'nt work.
2. Using a bool variable to deny the second access to receive function
I wish it is more clair now and anyone can help me.
Best regards.
Bookmarks