The client requests a shared network login status QNetworkCookieJar QNetworkCookie network module (seven)

A brief
1. Internet companies c / s architecture are done display and exchange data through the network interface or the browser request to access the server.
2. The client authenticates the server based account password link.
3. After verifying the identity of the server will be successful in writing client-side cookies back.
4.QNetworkAccessManager use cookies were QNetworkCookieJar management of all requests QNetworkAccessManager will use its internal QNetworkCookieJar
Here Insert Picture Description
5. If you want to use QWebEngineView interface access requires authentication URL, such as Taobao, Jingdong and so need to log in. At this time, the above-described need to share cookies in QWebEngineView. Specific methods are as follows:
QWebEngineView :: QWebEnginePage CookieStore :: :: :: QWebEngineProfile the setCookie (const QNetworkCookie & Cookie, Origin = const QUrl & QUrl ()); the url is the addition of authentication to access the cookies
II codes subclass CookieJar

#ifndef COOKIEJAR_H
#define COOKIEJAR_H

#include <QObject>
#include <QNetworkCookie>
#include <QDebug>
#include <QNetworkCookieJar>
#define pre true
#define prd true
//使用QNetworkAccessManager访问网络时,需要管理Cookie信息。
//Qt的QNetworkCookieJar类实现了一个简单的Cookie容器,但它只在内存中保留Cookie,该类的对象一旦被删除,里面的cookies也被丢弃。
//如果想存储在本地,必须继承该类然后自己实现存储。
class CookieJar : public QNetworkCookieJar
{
public:
    explicit CookieJar(QObject *parent = nullptr);
    ~CookieJar();
    void setCookies(const QList<QNetworkCookie> &cookieList);
    QList<QNetworkCookie> getCookies() const;
    void modifyCookies();

};

#endif // COOKIEJAR_H

#include "cookiejar.h"
CookieJar::~CookieJar()
{

}

void CookieJar::setCookies(const QList<QNetworkCookie> &cookieList)
{
    return setAllCookies(cookieList);
}

QList<QNetworkCookie> CookieJar::getCookies() const
{
    return allCookies();
}

void CookieJar::modifyCookies()
{
    QList<QNetworkCookie> cookieList = getCookies();
    for(int i=0;i<cookieList.size();i++){
        //配置cookie域名
        if(pre){
            cookieList.at(i).domain() = QString("pre");
        }
        else if(prd){
            cookieList.at(i).domain() = QString("prd");
        }
        //配置鉴权的cookie键值对
        if(cookieList.at(i).name() == QByteArray("auth0")){
            cookieList.at(i).value() = QByteArray("value0");
        }
        if(cookieList.at(i).name() == QByteArray("auth1")){
            cookieList.at(i).value() = QByteArray("value1");
        }
    }
    setCookies(cookieList);
}

Stating
network requests carried cookie and cookie response returned can be used Fiddler capture software to view it.
Fiddler software https://www.cnblogs.com/yyhh/p/5140852.html
specific configuration see https://blog.csdn.net/u010906468/article/details/104747464

Published 30 original articles · won praise 1 · views 1140

Guess you like

Origin blog.csdn.net/u010906468/article/details/104887324