如何使用Qt保存Cookie? [英] How do I save cookies with Qt?

查看:371
本文介绍了如何使用Qt保存Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将我的应用程式产生的Cookie储存到磁碟位置,例如 C:\Users\Username\AppData\Local\MyCompany\MyApp 。我实现了一个webview和几乎完成编码我的简单的浏览器最后要做的是保存cookie。

I am trying to save cookies that are produced by my app to disk location such as C:\Users\Username\AppData\Local\MyCompany\MyApp. I have implemented a webview and have pretty much finished coding my simple browser the final thing to do is save cookies.

我可以qDebug()我从webapp得到的cookies,他们显示cookie是正确的形成,但我是a)不确定从哪里去,b)

I am can qDebug() the cookies I get from the webapp and they show the cookies are formed correctly but I am a)unsure where to go from there and b) not 100% sure on how to make a subclass of the cookiejar class?

下面我在MainWindow构造函数中创建我的cookiejar对象

Below I create my cookiejar object in my MainWindow constructor

view = new QWebView(this);
jar = new QNetworkCookieJar;
view->page()->networkAccessManager()->setCookieJar(jar);

在我的回复完成槽中,我可以看到回复中包含的cookie,没有发生,我没有收到运行时错误。这里没有大量的东西,并看到一些帖子,其中的指令是做一个子类QNetworkCookieJar,但没有在Qt / C ++之前做过一个子类。

And in my replyfinished slot I can see the cookie contained in the reply and I attempt to save it but nothing happens and I receive no run time errors. There isn't a great deal of stuff out there on this and have seen a few posts where the instruction was to make a subclass QNetworkCookieJar but have not made a subclass in Qt/C++ before.

有一个简单的方法来存储cookie,我不是在寻找什么花哨。 Cookie只是确保在登录页面上勾选了一些复选框。

Is there a simple way to store cookies, I am not looking for anything fancy. The cookies just make sure some check boxes are ticked on the login page.

// SLOT that accepts the read data from the webpage
void MainWindow::slotReplyFinished(QNetworkReply *reply){

    if(reply->isFinished()){
        QVariant variantCookies = reply->header(QNetworkRequest::SetCookieHeader);
        QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(variantCookies);
        qDebug() << "Cookies reply: " << cookies;
        QNetworkCookie cookie; //Create a cookie



        jar = new QNetworkCookieJar;
        //view->page()->networkAccessManager()->setCookieJar(jar);
        jar->setCookiesFromUrl(cookies, reply->request().url());
        //qDebug() << "Saved cookies: " << jar->getAllCookies();
    }

    qDebug() << "Network reply: " << reply->errorString() << reply->error() << reply->request().url();
 }


推荐答案

您需要将QNetworkCookieJar并在该类中实现自己的持久存储。

You will need to subclass QNetworkCookieJar and in that class you should implement your own persistent storage.

class MyNetworkCookieJar : public QNetworkCookieJar {

public: 

bool saveCookiesToDisk() {
// .. my implementation
return true; // if i did
}

bool loadCookiesFromDisk() {
// .. load from disk
return false; // if unable to.
}
}

来自Qt项目的示例应用程序实现一个持久性cookie存储,它可能是一个很好的起点: http://qt.gitorious.org/qt /qt/trees/4.8/demos/browser

The sample application from Qt project implements a persistent cookie store, it could be a good starting point for you: http://qt.gitorious.org/qt/qt/trees/4.8/demos/browser

查看cookiejar.h和cookiejar.cpp

look at cookiejar.h and cookiejar.cpp

这篇关于如何使用Qt保存Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆