Solved Header invalid
-
Good morning, everyone,
I have a little problem. I'm trying to connect to an online game via Qt/C++. My problem and the server returns me an invalid header.
const QUrl url(this->ui->urlEdit->text()); //On récupère l'URL entrée par l'utilisateur. QNetworkRequest requete(url); //On crée notre requête avec l'url entrée par l'utilisateur QNetworkAccessManager *networkManager = new QNetworkAccessManager; //On crée le QNetworkAccessManager qui va traiter la requête QUrlQuery postData; postData.addQueryItem("kid", ""); postData.addQueryItem("language", "en"); postData.addQueryItem("autologin", "false"); postData.addQueryItem("credentials[email]", "xxxxx@gmail.com"); postData.addQueryItem("credentials[password]", "xxxxx"); requete.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QNetworkReply *reply = networkManager->post(requete, postData.toString(QUrl::FullyEncoded).toUtf8()); QVariant cookieVar = reply->header(QNetworkRequest::SetCookieHeader); qDebug() << reply->rawHeaderList(); // displays nothing the list is empty if (cookieVar.isValid()) { // invalid header does not fit here QList<QNetworkCookie> cookies = cookieVar.value<QList<QNetworkCookie> >(); foreach (QNetworkCookie cookie, cookies) { qDebug() << cookies; } }
-
@El_Professor said in Header invalid:
My problem and the server returns me an invalid header.
No, it hasn't returned anything yet.
QNetworkReply *reply = networkManager->post(requete, postData.toString(QUrl::FullyEncoded).toUtf8());
QVariant cookieVar = reply->header(QNetworkRequest::SetCookieHeader);You expect this code to be running synchron, but you can only read header after the finished signal (which you connect to your processing slot) has been emitted.
-
const QUrl url(this->ui->urlEdit->text()); //On récupère l'URL entrée par l'utilisateur. QNetworkRequest requete(url); //On crée notre requête avec l'url entrée par l'utilisateur QNetworkAccessManager *networkManager = new QNetworkAccessManager; //On crée le QNetworkAccessManager qui va traiter la requête QUrlQuery postData; postData.addQueryItem("kid", ""); postData.addQueryItem("language", "en"); postData.addQueryItem("autologin", "false"); postData.addQueryItem("credentials[email]", "jordan02820@gmail.com"); postData.addQueryItem("credentials[password]", "aqwzsxaze02820"); requete.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QNetworkReply *reply = networkManager->post(requete, postData.toString(QUrl::FullyEncoded).toUtf8()); QVariant cookieVar = reply->header(QNetworkRequest::SetCookieHeader); qDebug() << reply->rawHeaderList(); // displays nothing the list is empty if (cookieVar.isValid()) { // invalid header does not fit here QList<QNetworkCookie> cookies = cookieVar.value<QList<QNetworkCookie> >(); foreach (QNetworkCookie cookie, cookies) { qDebug() << cookies; } } // ------- On connect les signaux connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError))); connect(reply, SIGNAL(finished()), this, SLOT(replyFinished())); connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(progressionTelechargement(qint64, qint64)));
I had already connected the signals/slots but I hadn't thought about waiting for the signal to arrive.... Thank you for your answer. Have a nice day.
-
@El_Professor said in Header invalid:
QVariant cookieVar = reply->header(QNetworkRequest::SetCookieHeader);
qDebug() << reply->rawHeaderList(); // displays nothing the list is empty if (cookieVar.isValid()) { // invalid header does not fit here QList<QNetworkCookie> cookies = cookieVar.value<QList<QNetworkCookie> >(); foreach (QNetworkCookie cookie, cookies) { qDebug() << cookies; } }
I think this all should be in the slot connected to finished signal.
-
Yeah, that's what I did.
slot :
void MainWindow::replyFinished() { QMessageBox::information(this, "Fin de post", "L'envoi de données par POST a été effectué avec succès !"); QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); qDebug() << reply->rawHeaderList(); QVariant cookieVar = reply->header(QNetworkRequest::SetCookieHeader); if (cookieVar.isValid()) { // invalid header does not fit here QList<QNetworkCookie> cookies = cookieVar.value<QList<QNetworkCookie> >(); foreach (QNetworkCookie cookie, cookies) { qDebug() << cookies; } } close(); }