View Full Version : QNetworkCookieJar::allCookies() const is protected
svello
10th July 2009, 16:24
Hi all,
this is my first post on the forum and in these days are started my journeys in c++ and qt4.
My problem is with this line of code:
QList<QNetworkCookie> cookiesList(cookieJar->allCookies());
which gives the following error during make:
/usr/include/qt4/QtNetwork/qnetworkcookie.h: In member function ‘void HttpWindow::replyFinished(QNetworkReply*)’:
/usr/include/qt4/QtNetwork/qnetworkcookie.h:121: error: ‘QList<QNetworkCookie> QNetworkCookieJar::allCookies() const’ is protected
Maybe this is c++ matter than qt4; since QNetworkCookieJar::allCookies() is protected how assign its value to my cookiesList?
Thanks
wysota
10th July 2009, 17:07
You have to use QNetworkCookieJar::setCookiesFromUrl(). The method you mentioned is useful if you're implementing your own cookie jar and want to save cookies across sessions.
svello
10th July 2009, 17:31
Probably my first post was incomplete.
From the beginning: my application needs to login in a site and execute some opertations; so I have to store cookies:
manager = new QNetworkAccessManager;
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
cookieJar = new QNetworkCookieJar;
manager->setCookieJar(cookieJar);
I thought that once called manager->post(...) all cookies from website were stored in cookieJar so with cookieJar->allCookies() I could check them and, yes, reuse cookieJar for next operations in the site.
wysota
10th July 2009, 17:46
You have to store them yourself in the cookie jar, as far as I remember.
svello
10th July 2009, 17:57
You have to store them yourself in the cookie jar, as far as I remember.
And from where, since QNetworkReply seems not have any function related to cookies. Sorry wysota, but network argument is very tough in the documentation.
thanks
wysota
10th July 2009, 18:01
And from where, since QNetworkReply seems not have any function related to cookies.
Sure it does - QNetworkReply::header() with QNetworkRequest::SetCookieHeader.
svello
10th July 2009, 18:39
Sure it does - QNetworkReply::header() with QNetworkRequest::SetCookieHeader.
Now the only problem is converting a QVariant in QList<QNetworkCookie>.
I've tryed this
QList<QNetworkCookie> cookiesList(reply->header(QNetworkRequest::SetCookieHeader).toList()) ;
with no luck. It creates a QList<QVariant> and can't be used for cookiesList. Any tips?
wysota
11th July 2009, 10:31
QList<QNetworkCookie> cookiesList = qvariant_cast<QList<QNetworkCookie> >(reply->header(...));
svello
11th July 2009, 15:30
QList<QNetworkCookie> cookiesList = qvariant_cast<QList<QNetworkCookie> >(reply->header(...));
Thanks wysota, now it compiles correctly but as result I got an empty cookiesList; it is how no cookies were sent by the website. I have made a minimal example for testing purpose; have added a webview for checking site message and it says "Cookies are not enabled!":
#include <QApplication>
#include <QtGui>
#include <QtNetwork>
#include <QtWebKit>
class CookiesTest : public QDialog
{
Q_OBJECT
public:
CookiesTest(QWidget* parent = 0);
private slots:
void replyFinished(QNetworkReply*);
private:
QWebView* webView;
QLabel* label;
QNetworkAccessManager* manager;
};
CookiesTest::CookiesTest(QWidget* parent)
: QDialog(parent)
{
resize(640,480);
webView = new QWebView; // web page error messages
label = new QLabel; // displays num of cookies received
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(webView);
layout->addWidget(label);
manager = new QNetworkAccessManager;
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://www.tempesttech.com/cookies/cookietest2.asp")));
}
void CookiesTest::replyFinished(QNetworkReply* reply)
{
qDebug() << "Num of cookies: "
<< reply->header(QNetworkRequest::SetCookieHeader).toList(). size();
label->setText("Num of cookies: "
+ QVariant(reply->header(QNetworkRequest::SetCookieHeader).toList(). size()).toString());
webView->setContent(reply->readAll());
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
CookiesTest cookiesTest;
cookiesTest.exec();
}
#include "main.moc"
What am I doing wrong?
wysota
12th July 2009, 11:50
Did the website send you any cookies? Can you verify that with a network sniffer?
rexi
12th July 2009, 15:56
Maybe this is c++ matter than qt4; since QNetworkCookieJar::allCookies() is protected
Just for the record, to explain some basics of object orientation: Yes, this is a C++ matter. "protected" means that you can access this method only in classes derived from QNetworkCookieJar. "protected" methods are hidden, and thus, not accessible, from any other classes.
svello
12th July 2009, 17:01
Did the website send you any cookies? Can you verify that with a network sniffer?
Ok, solved! The problem was the wrong url; the right url for testing cookie ability is
http://www.tempesttech.com/cookies/cookietest1.asp
this link sets the cookie while that in my previous post checks only if it exists.
Many thanks, wysota.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.