How to use http cookies with Qt

ps:以下文章中文部分为软件翻译的结果,看的时候请自行分析

How to use http cookies with Qt

Overview

原文:

A cookie, also known as a web cookie, browser cookie, and HTTP cookie, is a piece of text stored by a user's web browser. A cookie can be used for authentication, storing site preferences, shopping cart contents, the identifier for a server-based session, or anything else that can be accomplished through storing text data.A cookie consists of one or more name-value pairs containing bits of information, which may be encrypted for information privacy and data security purposes. The cookie is sent as an HTTP header by a web server to a web browser and then sent back unchanged by the browser each time it accesses that server.

翻译:

一cookie,也被称为一个Web浏览器的cookie,cookie和HTTP,是一块存储在用户浏览器的文本。Cookie可以用于认证,存储网站的喜好,购物车的内容,为基于服务器的会话标识符,或其他任何可以通过存储文本数据来完成的。一个cookie包含一个或多个名称-值对包含的信息,它可以被加密的信息隐私和数据安全的目的。Cookie是由Web服务器网页浏览器HTTP标头发送然后送回不变的浏览器,每次访问服务器。

Code

原文:

The Qt Network module offers the class QNetworkCookieJar which can be used with QNetworkAccessManager to manage cookies in Qt applications.The following class will show you how to send a post request to a URL and read the cookies stored in the header.

翻译:

QT网络模块提供的类qnetworkcookiejar可与qnetworkaccessmanager在Qt应用程序管理的cookies。下面的类将告诉你如何发送POST请求的URL和读取存储在头的cookie。

#ifndef COOKIESHANDLER_H
#define COOKIESHANDLER_H
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkCookieJar>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QDebug>
 
class cookiesHandler: public QObject{
    Q_OBJECT
 
public:
    cookiesHandler(QObject *parent = 0) : QObject(parent){
        mManager = new QNetworkAccessManager(this);
        mManager->setCookieJar(new QNetworkCookieJar(this));
        connect(mManager, SIGNAL(finished(QNetworkReply*)), SLOT(replyFinished(QNetworkReply*)));
    }
 
    void sendPostRequest(const QUrl &url, const QByteArray &data){
        mUrl = url;
        QNetworkRequest r(mUrl);
        mManager->post(r, data);
    }
 
    virtual ~cookiesHandler(){}
 
private slots:
    void replyFinished(QNetworkReply *reply){
        if (reply->error() != QNetworkReply::NoError){
            qWarning() << "ERROR:" << reply->errorString();
            return;
        }
 
        QList<QNetworkCookie>  cookies = mManager->cookieJar()->cookiesForUrl(mUrl);
        qDebug() << "COOKIES for" << mUrl.host() << cookies;
    }
 
private:
    QNetworkAccessManager *mManager;
    QUrl mUrl;
};
 
 
#endif // COOKIESHANDLER_H

The previous class can be used as follow:

QUrl url("https://yourwebsite.com"); // https and http are both allowed.
QByteArray postData;
postData.append("username=myUsername&password=myPAss&hiddenFields=ifAny");
c.sendPostRequest(url, postData);

The cookie can be sent back to the server with another request in this way:

QList<QNetworkCookie>  cookies = mManager->cookieJar()->cookiesForUrl(mUrl);
QVariant var;
var.setValue(cookies);
 
QNetworkRequest r(QUrl("http://www.developer.nokia.com/Profile/"));
r.setHeader(QNetworkRequest::CookieHeader, var);
mManager->get(r);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值