Kempo
29th February 2012, 11:29
Hello!
Objective: To load different pages of one site, with the ability to use previously issued cookie
The simplest application loads the page from localhost.
#include <QtCore>
#include <QApplication>
#include <QDebug>
#include <QtNetwork>
class MyCookieJar : public QNetworkCookieJar
{
public:
QList<QNetworkCookie> getAllCookies() { return allCookies(); }
};
class CookiesTest : public QObject
{
Q_OBJECT
public:
CookiesTest(QWidget* parent = 0);
private slots:
void replyFinished(QNetworkReply*);
private:
QNetworkAccessManager* manager;
MyCookieJar *cookieJar;
};
CookiesTest::CookiesTest(QWidget* parent)
{
manager = new QNetworkAccessManager;
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
cookieJar = new MyCookieJar;
manager->setCookieJar(cookieJar);
manager->get(QNetworkRequest(QUrl("http://test1.ru/test/cookie.php")));
manager->get(QNetworkRequest(QUrl("http://test1.ru/test/cookie.php")));
}
void CookiesTest::replyFinished(QNetworkReply* reply)
{
qDebug() << reply->readAll();
qDebug() << "getAllCookies: " << cookieJar->getAllCookies();
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
CookiesTest cookiesTest;
return app.exec();
}
#include "main.moc"in the file .pro: QT += network
File cookie.php shows number of visits that page on the analysis of the cookie
<?php
if (isset($_COOKIE['Mortal'])) $cnt = $_COOKIE['Mortal'] + 1;
else $cnt = 1;
setcookie("Mortal",$cnt,0x6FFFFFFF);
echo "NUM: [".@$_COOKIE['Mortal']."]";
?>
Output:
"NUM: []"
getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
"NUM: []"
getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
Although to be the second time in brackets the number 1 and the second getAllCookies: Mortal = 2
Tell me please, where was the mistake, what's wrong? Why cookies are not stored on the 2nd request
Objective: To load different pages of one site, with the ability to use previously issued cookie
The simplest application loads the page from localhost.
#include <QtCore>
#include <QApplication>
#include <QDebug>
#include <QtNetwork>
class MyCookieJar : public QNetworkCookieJar
{
public:
QList<QNetworkCookie> getAllCookies() { return allCookies(); }
};
class CookiesTest : public QObject
{
Q_OBJECT
public:
CookiesTest(QWidget* parent = 0);
private slots:
void replyFinished(QNetworkReply*);
private:
QNetworkAccessManager* manager;
MyCookieJar *cookieJar;
};
CookiesTest::CookiesTest(QWidget* parent)
{
manager = new QNetworkAccessManager;
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
cookieJar = new MyCookieJar;
manager->setCookieJar(cookieJar);
manager->get(QNetworkRequest(QUrl("http://test1.ru/test/cookie.php")));
manager->get(QNetworkRequest(QUrl("http://test1.ru/test/cookie.php")));
}
void CookiesTest::replyFinished(QNetworkReply* reply)
{
qDebug() << reply->readAll();
qDebug() << "getAllCookies: " << cookieJar->getAllCookies();
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
CookiesTest cookiesTest;
return app.exec();
}
#include "main.moc"in the file .pro: QT += network
File cookie.php shows number of visits that page on the analysis of the cookie
<?php
if (isset($_COOKIE['Mortal'])) $cnt = $_COOKIE['Mortal'] + 1;
else $cnt = 1;
setcookie("Mortal",$cnt,0x6FFFFFFF);
echo "NUM: [".@$_COOKIE['Mortal']."]";
?>
Output:
"NUM: []"
getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
"NUM: []"
getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
Although to be the second time in brackets the number 1 and the second getAllCookies: Mortal = 2
Tell me please, where was the mistake, what's wrong? Why cookies are not stored on the 2nd request