PDA

View Full Version : Problems with QNetworkAccessManager



pfid
15th November 2009, 14:26
Hi all,

I'm having some problems with QNetworkAccessManager (qt 4.5.2, windows 7). i try to develop a simple widget that is supposed to connect to google calendar. Basically all functionality is a set of post() and request() function calls to the appropriate google calendar urls. While this basically works, i get random http errors at random points of execution, and i cant seem to find out whats wrong.

my current code is supposed to do a 'login', consisting of 3 requests:
1) post() username & password to login url
* receive authorization token, use this for all following requests
2) get() to calendar list url, to receive a list of calendars
* receive calender list
3) get() to event list url
* receive event list

sometimes, all steps are executed, and sometimes i get http400/Bad Request, http 399/Data corrupted or even http status code 0 along with an empty reply at random points. now, i think it might have something to do with the fact that i fire requests in QNetworkAccessManager's finished() slot, but i dont know how to do it right.

now for some code:



MainClass::MainClass()
{
lastRequest = 0;
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuth enticator*)), this, SLOT(onAuthenticationRequired(QNetworkReply*,QAuth enticator*)));
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
connect(manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,Q Authenticator*)), this, SLOT(onProxyAuthenticationRequired(QNetworkProxy,Q Authenticator*)));
connect(manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(onSslErrors(QNetworkReply*,QList<QSslError>)));

...


prepare Requests:


int MainClass::prepare(const QString& url)
{
delete lastRequest;
lastRequest = new QNetworkRequest(QUrl(url));

if (!authToken.isEmpty())
{
lastRequest->setRawHeader("Authorization", authToken);
lastRequest->setRawHeader("GData-Version", "2");
}

return success;
}


do login:



int MainClass::login(const QString& aEmail, const QString& aPassword)
{
DataPacket data; // request body

prepare(urls[urlLogin]); // s.o.

/* prepare body */

manager->post(*lastRequest, data.getData() /* == QByteArray */);

return success;
}



the other requests are simple GETs on the apropriate feed-urls:



int MainClass::getCalendarList()
{
...
/* some error checks */

prepare(urls[urlCalendarListFull]);

manager->get(*lastRequest);

return success;
}


finished-slot:



void MainClass::onFinished(QNetworkReply* reply)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute ).toInt();

qDebug() << "FINISHED" << statusCode;

switch (statusCode)
{
case 200: // OK
{
// process reply

if (!isLoggedIn())
storeAuthorization(reply);

switch (state)
{
case stGetCalendars:
{
parseCalendarList(reply);
break;
}
case stGetEvents:
{
parseEventList(reply);
break;
}
}

break;
}

case 302: // redirect (Location: [URL])
{
qDebug() << "redirected to" << locationOf(reply);
prepare(locationOf(reply));
manager->get(*lastRequest);

break;
}

case 400: // bad request
{
QMessageBox::warning(this, "Error", "Network error (HTTP400/Bad Request)");
break;
}

case 403: // permission denied
{
QMessageBox::warning(this, "Permission denied", "Username or password invalid");
break;
}

default:
{
QByteArray replyData = reply->readAll();
qDebug() << "Unknown response" << replyData;
break;
}
}

reply->deleteLater();

// do anything afterwards?

after(); // <-- next request via manager->get()

qDebug() << "request done";
}


does someone know what might be wrong? :(

pfid
18th November 2009, 20:51
anyone please? :(