日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

CORS跨域限制以及预请求验证(C++ Qt框架实现)

發(fā)布時(shí)間:2025/3/15 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CORS跨域限制以及预请求验证(C++ Qt框架实现) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

?

理論

演示及代碼


理論

跨域默認(rèn)情況下允許方法:GET、HEAD、POST

默認(rèn)情況下允許的Content-Type:text/plain、multipart/form-data、application/x-www-form-urlencoded

如果要使用用戶自定義頭,要在被請求的服務(wù)器HTTP響應(yīng)消息中加入:

Access-Control-Allow-Headers: X-Test-Cors

這個(gè)X-Test-Cors就是自定義消息頭

如果要添加新的跨域方法要添加如下消息:

Access-COntrol-Allow-Methods: PUT, Delete

如果要設(shè)置Option這個(gè)預(yù)請求時(shí)間,則要加如下消息頭:

Access-Control-Max-Age: 1000

?

演示及代碼

現(xiàn)在有2個(gè)頁面,都在127.0.0.1的機(jī)器上,

一個(gè)服務(wù)是80端口,一個(gè)服務(wù)是81端口,

80端口通過JavaScript獲取81端口跨域服務(wù)!

現(xiàn)在使用自定義消息頭!

程序運(yùn)行截圖如下:

此時(shí)在81上添加允許X-Test-Cors的響應(yīng)消息:

運(yùn)行這個(gè)時(shí)候就沒有報(bào)錯(cuò)了,并且瀏覽器有預(yù)處理和響應(yīng)了!

看下這Option預(yù)處理頭和Post請求:

這里通過設(shè)置Max-Age設(shè)置預(yù)處理頭:

運(yùn)行截圖如下:

程序結(jié)構(gòu)如下:

widget.h

#ifndef WIDGET_H #define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE class QTcpServer; class QTcpSocket; QT_END_NAMESPACEnamespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();protected slots:void newConnectionSlot80();void newConnectionSlot81();void errorStringSlot80();void errorStringSlot81();void sendMsg80();void sendMsg81();private:Ui::Widget *ui;QTcpServer *m_tcpServer80;QTcpSocket *m_tcpSocket80;QTcpServer *m_tcpServer81;QTcpSocket *m_tcpSocket81; };#endif // 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_tcpServer80 = new QTcpServer(this);m_tcpServer80->listen(QHostAddress::Any, 80);m_tcpServer81 = new QTcpServer(this);m_tcpServer81->listen(QHostAddress::Any, 81);connect(m_tcpServer80, SIGNAL(newConnection()), this, SLOT(newConnectionSlot80()));connect(m_tcpServer81, SIGNAL(newConnection()), this, SLOT(newConnectionSlot81()));connect(m_tcpServer80, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(errorStringSlot80()));connect(m_tcpServer81, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(errorStringSlot81()));}Widget::~Widget() {delete ui;m_tcpServer80->close();m_tcpServer81->close(); }void Widget::newConnectionSlot80() {qDebug() << "newConnectionSlot80() called";m_tcpSocket80 = m_tcpServer80->nextPendingConnection();connect(m_tcpSocket80, SIGNAL(readyRead()), this, SLOT(sendMsg80())); }void Widget::newConnectionSlot81() {qDebug() << "newConnectionSlot81() called";m_tcpSocket81 = m_tcpServer81->nextPendingConnection();connect(m_tcpSocket81, SIGNAL(readyRead()), this, SLOT(sendMsg81())); }void Widget::errorStringSlot80() {qDebug() << m_tcpServer80->errorString(); }void Widget::errorStringSlot81() {qDebug() << m_tcpServer81->errorString(); }void Widget::sendMsg80() { // QString contentStr = "<html>" // "<head>" // "<title>" // "Socket 80" // "</title>" // "</head>" // "<body>" // "<h1>Socket 80</h1>" // "<script src=\"http://127.0.0.1:81\"></script>" // "</body>" // "</html>";QString contentStr = "<html>""<head>""<title>""Socket 80""</title>""</head>""<body>""<h1>Socket 80</h1>""<script>"" fetch('http://127.0.0.1:81',{\n"" method: 'POST',\n"" headers:{\n"" 'X-Test-Cors': '123'\n"" }\n"" })\n""</script>""</body>""</html>";//send msgQString str = "HTTP/1.1 200 OK\r\n";str.append("Server:nginx\r\n");str.append("Content-Type:text/html\r\n");str.append("Connection:keep-alive\r\n");str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));str.append(contentStr);qDebug() << str;m_tcpSocket80->write(str.toStdString().c_str()); }void Widget::sendMsg81() {QString contentStr = "<html>""<head>""<title>""Socket 81""</title>""</head>""<body>""<h1>Socket 81</h1>""</body>""</html>";//send msgQString str = "HTTP/1.1 200 OK\r\n";str.append("Server:nginx\r\n");str.append("Content-Type:text/html\r\n");str.append("Connection:keep-alive\r\n");str.append("Access-Control-Allow-Origin: *\r\n");str.append("Access-Control-Allow-Headers: X-Test-Cors\r\n");str.append("Access-Control-Max-Age: 1000\r\n");str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));str.append(contentStr);qDebug() << str;m_tcpSocket81->write(str.toStdString().c_str()); }

?

程序打包下載:

https://github.com/fengfanchen/Qt/tree/master/HTTPCORSTest

總結(jié)

以上是生活随笔為你收集整理的CORS跨域限制以及预请求验证(C++ Qt框架实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。