Qt网络编程之使用cookie

Posted blogpuppet

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt网络编程之使用cookie相关的知识,希望对你有一定的参考价值。

所使用的Qt版本:5.14.0

本例子基于这篇博客中的例子:《Qt使用HTTPS协议访问网站

 

接收Cookies:

Cookie由返回的QNetworkReply获取,Qt已经做得很好了,我们只需要调用相关函数即可,像其他响应消息的头也可以由类似的方式获取。

#include <QCoreApplication>
#include "TestNet.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TestNet One;
    One.GetWeb();

    return a.exec();
}

//main.cpp
#include <QtNetwork>
#include <iostream>
#include <QFile>


class TestNet : public QObject
{
    Q_OBJECT
public:
    QNetworkAccessManager *m_manager = NULL;
    QNetworkRequest *m_request = NULL;

    //访问网站
    void GetWeb()
    {
        //建立新对象
        m_manager = new QNetworkAccessManager();
        m_request = new QNetworkRequest(QUrl("https://gitee.com/"));

        //设置SSL连接
        QSslConfiguration config;
        QSslConfiguration conf = m_request->sslConfiguration();
        conf.setPeerVerifyMode(QSslSocket::VerifyNone);
        conf.setProtocol(QSsl::TlsV1SslV3);
        m_request->setSslConfiguration(conf);

        //连接槽
        QObject::connect(m_manager,&QNetworkAccessManager::finished,this,&TestNet::GetWebFinished);

        //发送Get请求
        m_manager->get(*m_request);
    }

    //访问后做的操作
    void GetWebFinished(QNetworkReply *reply)
    {
        //转换为utf-8编码,这样就可以显示中文了
        QTextCodec *codec = QTextCodec::codecForName("utf8");
        QString getData = codec->toUnicode(reply->readAll());

        //获取Cookies
        if(reply->hasRawHeader("Set-Cookie"))
        {
            QByteArray cookie = reply->rawHeader("Set-Cookie");
            std::cout<<cookie.toStdString();
        }

    }

};

//TestNet.h

 

发送Cookies:

首先我们先看一下用Fiddler抓包软件截取的Http请求包。

POST https://gitee.com/check_user_login HTTP/1.1
Host: gitee.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0
Accept: */*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate, br
Referer: https://gitee.com/login
X-CSRF-Token: GbYJaqAJEXGRYoNtYXUlzdw06JNrqC1FGO7wJO8L+J8=
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 22
Origin: https://gitee.com
Connection: keep-alive
Cookie: user_locale=zh-CN; oschina_new_user=false; Hm_lvt_24f17767262929947cc3631f99bfd274=1587994350,1587994363,1587994651,1587995075; remote_way=http; gitee-session-n=cVdXY3BBSG91a2JSWXRsR0gybVNZTXB6UUZoZU11dzdnNERRVjJNUmRSUzQ0NzZ5YkFHNjM5cG1Lbk82RHYrclVaQzRURjZIbFRsSGkybm1FQmdVR1M2dFJPZmhFdXNCK0JrRk9DVEVBQXdnVXF2V2tuYWw3aXRURUlEWkV6aXc0VUlwRGtJb1dCd0g2NFVxeW1IMC9MSHU2dnJHck1kY213ZEdDcWFFOU1OZTZtTWdKN1VkYVNTdGxVaUpIMUtpLS02K0Z1VzBFOHQyQ1FXWXpSYlhSZEVBPT0%3D--631c43af162f79e53290d6e86ac1ada72f92ffed; tz=Asia%2FShanghai; Hm_lpvt_24f17767262929947cc3631f99bfd274=1587995076

这是我在登陆Gitee网站时浏览器发出的POST请求头,那么请问,这个请求头中有多少个cookie?

答案是:有7个(有多少个等号就有多少个cookie,每个cookie用 ; 分隔开来)

至少按照Qt的方式是这样理解的(狗头

所以,我们一般叫这玩意作:cookies

而每一个cookie有两个基本属性,等号之前的属性名称为“name”,等号后面的属性为“value”;

 

代码如下:

#include <QCoreApplication>
#include "TestNet.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TestNet One;
    One.GetWeb();

    return a.exec();
}

//main.cpp
#include <QtNetwork>
#include <iostream>
#include <QFile>


class TestNet : public QObject
{
    Q_OBJECT
public:
    QNetworkAccessManager *m_manager = NULL;
    QNetworkRequest *m_request = NULL;
    QList<QNetworkCookie> Web_cookies;  //这么做的意图很明显了吧,QNetworkCookie是每一个cookie,QList是cookies

    //访问网站
    void GetWeb()
    {
        //建立新对象
        m_manager = new QNetworkAccessManager();
        m_request = new QNetworkRequest(QUrl("https://gitee.com/"));

        //设置SSL连接
        QSslConfiguration config;
        QSslConfiguration conf = m_request->sslConfiguration();
        conf.setPeerVerifyMode(QSslSocket::VerifyNone);
        conf.setProtocol(QSsl::TlsV1SslV3);
        m_request->setSslConfiguration(conf);

        //设置cookies
        Web_cookies.push_back(QNetworkCookie("Name","Value"));
        Web_cookies.push_back(QNetworkCookie("Test","Net"));

        //向请求头里加入cookies
        QVariant var;
        var.setValue(Web_cookies);
        m_request->setHeader(QNetworkRequest::CookieHeader,var);

        //连接槽
        QObject::connect(m_manager,&QNetworkAccessManager::finished,this,&TestNet::GetWebFinished);

        //发送Get请求
        m_manager->get(*m_request);
    }

    //访问后做的操作
    void GetWebFinished(QNetworkReply *reply)
    {
        //NULL
    }

};

//TestNet.h

 



然后呢,发送的Http请求头如下:

GET https://gitee.com/ HTTP/1.1
Host: gitee.com
Cookie: Name=Value; Test=Net
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,en,*
User-Agent: Mozilla/5.0

有什么问题就不要留言了,不是我无情,而是我是“全宿制”学生党,我也很无奈。

 

以上是关于Qt网络编程之使用cookie的主要内容,如果未能解决你的问题,请参考以下文章

我的QT Creator学习笔记(三十四)——网络编程之HTTP与FTP

Qt编程遇到的问题,我在qt中直接使用C语言的程序片段,有问题 ,求解

QT 实用代码片段

面向面试编程代码片段之GC

js代码片段: utils/lcoalStorage/cookie

Qt之QNetworkProxy(网络代理)