HTTP之Cookie和Session(C++ Qt框架实现)
目錄
?
?
基本概念
博主例子
?
基本概念
在HTTP服務(wù)器中,響應(yīng)頭可以設(shè)置Cookie;
? ? ? ? ? ? ? 1. 通過Set-Cookie設(shè)置;
? ? ? ? ? ? ? 2.? 下次請求頭會自動帶上;
? ? ? ? ? ? ? 3. Cookie是鍵值對,可以設(shè)置多個;
?
Cookie屬性:
? ? ? ? ? ? ? 1. 通過max-age和expires設(shè)置過期時間;
? ? ? ? ? ? ? 2. Secure只在https的時候發(fā)送;
? ? ? ? ? ? ? 3. HttpOnly無法通過document.cookie訪問
?
HttpOnly是為了避免CSRF攻擊,網(wǎng)頁注入腳本,或者使用域名獲取用戶Cookie所以要禁止重要的數(shù)據(jù),通過JavaScript進(jìn)行訪問,能夠保證用戶數(shù)據(jù)安全;
?
下面來說明下Cookie和Session
? ? ? ? ? ? ? 1. Cookie?不等于 Session;
? ? ? ? ? ? ? 2. Session?有很多種實現(xiàn)方法;
? ? ? ? ? ? ? 3. 其中一個就是用Cookie?來保存 Session;
?
?
博主例子
如下偽代碼:設(shè)置多個Cookie
?
運(yùn)行截圖如下:
其中對應(yīng)的代碼為:
這里有一個max-age。改變服務(wù)器為wuCookie
如下:
10s后,在此發(fā)起請求,看看瀏覽器的請求頭!
可以看到ID已經(jīng)過期,只剩下name了。
?
下面來演示下把一個域名下的所有子域名都設(shè)置Cookie;
這里只要設(shè)置domain=xxx.com這樣a.xxx.com和b.xxx.com都可以使用一個Cookie。
我這里是Win7 先設(shè)置一個Host
對應(yīng)的代碼如下:
運(yùn)行截圖如下:
下面是www的二級域名:
程序結(jié)構(gòu)如下:
widget.h
main.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QDebug> #include <QTcpServer> #include <QTcpSocket>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);m_tcpServer = new QTcpServer(this);m_tcpServer->listen(QHostAddress::Any, 80);connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(btnClicked()));connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));connect(m_tcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(errorStringSlot())); }Widget::~Widget() {delete ui;m_tcpServer->close(); }void Widget::newConnectionSlot() {qDebug() << "newConnectionSlot80() called";m_tcpSocket = m_tcpServer->nextPendingConnection();sendMsg(); }void Widget::errorStringSlot() {qDebug() << m_tcpServer->errorString(); }int choose = 0;void Widget::btnClicked() {if(choose == 0){ui->pushButton->setText("無Cookie");choose++;}else if(choose == 1){ui->pushButton->setText("domin相關(guān)");choose++;}else{ui->pushButton->setText("有max-age");choose = 0;} }void Widget::sendMsg() {QString contentStr;QString str;contentStr = "<html>""<head>""<title>""Socket 80""</title>""</head>""<body>""<h1>Socket 80</h1>""</body>""<script>"" console.log(document.cookie)""</script>""</html>";if(ui->pushButton->text() == "有max-age"){str = "HTTP/1.1 200 OK\r\n";str.append("Server:nginx\r\n");str.append("Content-Type:text/html\r\n");str.append("Access-Control-Allow-Origin: *\r\n");str.append("Set-Cookie: id=123456; max-age=10\r\n");str.append("Set-Cookie: name=123456\r\n");str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));str.append(contentStr);}else if(ui->pushButton->text() == "無Cookie"){str = "HTTP/1.1 200 OK\r\n";str.append("Server:nginx\r\n");str.append("Content-Type:text/html\r\n");str.append("Access-Control-Allow-Origin: *\r\n");str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));str.append(contentStr);}else{str = "HTTP/1.1 200 OK\r\n";str.append("Server:nginx\r\n");str.append("Content-Type:text/html\r\n");str.append("Access-Control-Allow-Origin: *\r\n");str.append("Set-Cookie: id=123456;domain=it1995.cn\r\n");str.append("Set-Cookie: name=123456\r\n");str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));str.append(contentStr);}m_tcpSocket->write(str.toStdString().c_str()); }?
源碼打包下載地址:
https://github.com/fengfanchen/Qt/tree/master/CookieHTTPDemo
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的HTTP之Cookie和Session(C++ Qt框架实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QML笔记-使用connect界面数据交
- 下一篇: HTTP之Redirect和Locati