为啥我的自定义 cookiejar 不起作用?
Posted
技术标签:
【中文标题】为啥我的自定义 cookiejar 不起作用?【英文标题】:Why is my custom cookiejar not working?为什么我的自定义 cookiejar 不起作用? 【发布时间】:2014-01-12 02:33:35 【问题描述】:我需要通过 qnetworkcookiejar 从本地磁盘加载 cookie,但是尝试时它不起作用。这是我的代码:
QNetworkCookieJar *cookieJar = new QNetworkCookieJar;
QFile file("d:/cookie.txt");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
while (!in.atEnd())
QString line = in.readLine();
QList<QNetworkCookie> cookie = QNetworkCookie::parseCookies(line.toLocal8Bit());
foreach (QNetworkCookie c, cookie)
qDebug() << cookieJar->insertCookie(c);
QUrl url = QUrl(QString("http://www.renren.com/"));
QWebView view;
QWebInspector inspector;
inspector.setPage(view.page());
view.settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
view.settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, false);
QNetworkAccessManager *manager = new QNetworkAccessManager;
manager->setCookieJar(cookieJar);
view.page()->setNetworkAccessManager(manager);
inspector.setVisible(true);
view.show();
QEventLoop loop;
QObject::connect(&view, SIGNAL(loadFinished(bool)),
&loop, SLOT(quit()));
view.load(QString("http://www.renren.com"));
d:/cookie.txt的内容:
anonymid=hqazrdfcujqlcw; expires=Thu, 10-Jan-2019 14:50:55 GMT; domain=.renren.com; path=/
depovince=ZGQT; expires=Tue, 14-Jan-2014 14:50:55 GMT; domain=.renren.com; path=/
jebecookies=1e67b394-5310-4ba3-899c-4bfe76a48eaf|||||; domain=.renren.com; path=/
_r01_=1; expires=Tue, 06-Jan-2015 14:50:55 GMT; domain=.renren.com; path=/
JSESSIONID=abc-myf8pCJA6hWBmYbou; domain=www.renren.com; path=/
ick_login=f8c346c3-cfb3-48d6-9420-1762fab34171; domain=.renren.com; path=/
UID=649f302-216.156.147.49-1389451858; expires=Fri, 01-Jan-2016 14:50:58 GMT; domain=.scorecardresearch.com; path=/
UIDR=1389451858; expires=Fri, 01-Jan-2016 14:50:58 GMT; domain=.scorecardresearch.com; path=/
_de=040F82A18DD5747B2955652844C08B9F34DF20B0B3AA6FF7; expires=Tue, 06-Jan-2015 14:51:37 GMT; domain=.renren.com; path=/
p=1926b0e5257c8146601b4d063586f4c94; expires=Mon, 10-Feb-2014 14:51:37 GMT; domain=.renren.com; path=/
ap=175034604; expires=Mon, 10-Feb-2014 14:51:37 GMT; domain=.renren.com; path=/
first_login_flag=1; domain=.renren.com; path=/
t=61569f309ee6301116241f66ab6d018e4; domain=.renren.com; path=/
t=b83a0150ef41ddebd7f3937b11886a70; domain=.renren.com; path=/xtalk/
societyguester=61569f309ee6301116241f66ab6d018e4; domain=.renren.com; path=/
id=175034604; domain=.renren.com; path=/
xnsid=7e369346; domain=.renren.com; path=/
loginfrom=syshome; domain=.renren.com; path=/
loginfrom=null; domain=.renren.com; path=/
feedType=175034604_hot; expires=Mon, 10-Feb-2014 14:51:38 GMT; domain=.www.renren.com; path=/
an_slot=115; domain=ebp.renren.com; path=/ebpn
today=22.51; expires=Sat, 11-Jan-2014 15:59:59 GMT; domain=wpi.renren.com; path=/wtalk/
当我加载 www.renren.com 时,webinspector 中似乎没有 Cookie。有人可以帮我吗?
【问题讨论】:
【参考方案1】:如果你想在磁盘上存储 cookie,你应该按照 Qt 文档继承 QNetworkCookieJar
。此外,当您使用 QSettings 时,加载和保存非常简单。这是我的 QNetworkCookieJar
实现的永久版本:
.h
class CMyNetworkCookieJar : public QNetworkCookieJar
Q_OBJECT
public:
CMyNetworkCookieJar(QObject *p_pParent = NULL );
~CMyNetworkCookieJar( void );
bool setCookiesFromUrl(const QList<QNetworkCookie> & p_grCookieList, const QUrl & p_grUrl);
QList<QNetworkCookie> cookiesForUrl( const QUrl & p_grUrl ) const;
void loadCookies( void );
void saveCookies( void );
protected:
QSettings * m_pCookieStorage;
;
.cpp
CMyNetworkCookieJar::CMyNetworkCookieJar( QObject * p_pParent )
: QNetworkCookieJar( p_pParent )
m_pCookieStorage = new QSettings( QSettings::IniFormat,
QSettings::UserScope,
QApplication::organizationName(),
QApplication::applicationName() );
loadCookies();
CMyNetworkCookieJar::~CMyNetworkCookieJar()
saveCookies();
m_pCookieStorage->sync();
delete m_pCookieStorage;
bool CMyNetworkCookieJar::setCookiesFromUrl(const QList<QNetworkCookie> & p_grCookieList, const QUrl &p_grUrl)
return QNetworkCookieJar::setCookiesFromUrl( p_grCookieList, p_grUrl );
QList<QNetworkCookie> CMyNetworkCookieJar::cookiesForUrl(const QUrl &p_grUrl) const
return QNetworkCookieJar::cookiesForUrl( p_grUrl );
void CMyNetworkCookieJar::loadCookies()
QByteArray grCookies = m_pCookieStorage->value( "cookies" ).toByteArray();
QList< QNetworkCookie > grCookieList = QNetworkCookie::parseCookies( grCookies );
setAllCookies( grCookieList );
void CMyNetworkCookieJar::saveCookies()
QList<QNetworkCookie> grCookieList = allCookies();
QByteArray grCookies;
foreach( QNetworkCookie grCookie, grCookieList )
if ( grCookie.isSessionCookie() == false )
grCookies.append( grCookie.toRawForm() );
grCookies.append( "\n" );
m_pCookieStorage->setValue( "cookies", grCookies );
【讨论】:
以上是关于为啥我的自定义 cookiejar 不起作用?的主要内容,如果未能解决你的问题,请参考以下文章
不知道为啥我的自定义 UICollectionViewCell 不起作用?