为什么我的自定义cookiejar不起作用?

时间:2014-01-12 02:33:35

标签: qt cookies qtwebkit qnetworkaccessmanager

我需要通过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 个答案:

答案 0 :(得分:1)

在Qt文档记录之后,如果要在磁盘上存储cookie,则应继承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 );
}