QNetworkCookieJar::allCookies() const is protected
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:
Code:
QList<QNetworkCookie> cookiesList(cookieJar->allCookies());
which gives the following error during make:
Quote:
/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
Re: QNetworkCookieJar::allCookies() const is protected
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.
Re: QNetworkCookieJar::allCookies() const is protected
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:
Code:
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.
Re: QNetworkCookieJar::allCookies() const is protected
You have to store them yourself in the cookie jar, as far as I remember.
Re: QNetworkCookieJar::allCookies() const is protected
Quote:
Originally Posted by
wysota
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
Re: QNetworkCookieJar::allCookies() const is protected
Quote:
Originally Posted by
svello
And from where, since
QNetworkReply seems not have any function related to cookies.
Sure it does - QNetworkReply::header() with QNetworkRequest::SetCookieHeader.
Re: QNetworkCookieJar::allCookies() const is protected
Quote:
Originally Posted by
wysota
Now the only problem is converting a QVariant in QList<QNetworkCookie>.
I've tryed this
Code:
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?
Re: QNetworkCookieJar::allCookies() const is protected
Code:
QList<QNetworkCookie> cookiesList = qvariant_cast<QList<QNetworkCookie> >(reply->header(...));
Re: QNetworkCookieJar::allCookies() const is protected
Quote:
Originally Posted by
wysota
Code:
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!":
Code:
#include <QApplication>
#include <QtGui>
#include <QtNetwork>
#include <QtWebKit>
{
Q_OBJECT
public:
private slots:
void replyFinished(QNetworkReply*);
private:
QWebView* webView;
QNetworkAccessManager* manager;
};
CookiesTest
::CookiesTest(QWidget* parent
){
resize(640,480);
webView = new QWebView; // web page error messages
label
= new QLabel;
// displays num of cookies received 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[])
{
CookiesTest cookiesTest;
cookiesTest.exec();
}
#include "main.moc"
What am I doing wrong?
Re: QNetworkCookieJar::allCookies() const is protected
Did the website send you any cookies? Can you verify that with a network sniffer?
Re: QNetworkCookieJar::allCookies() const is protected
Quote:
Originally Posted by
svello
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.
Re: QNetworkCookieJar::allCookies() const is protected
Quote:
Originally Posted by
wysota
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.