日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

HTTP中CORS跨域请求的实现(C++|Qt框架实现)

發布時間:2025/3/15 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HTTP中CORS跨域请求的实现(C++|Qt框架实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

背景

關鍵

演示及源碼


?

背景

HTTP中CORS跨域請求,可以獲取其他服務器的數據;

這里用Qt框架實現!

邏輯如下:

瀏覽器訪問127.0.0.1:80端口!80端口請求了127.0.0.1:81的數據!

這里都是指資源!

?

關鍵

需要在127.0.0.1:81服務器中的HTTP數據包頭添加如下數據:

Access-Control-Allow-Origin: *\r\n

?

演示及源碼

上面的script就是向127.0.0.1:81端口的數據:

請求頭如下:

程序結構如下:

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 <QTcpServer> #include <QDebug> #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>";//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("Access-Control-Allow-Origin: *\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(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));str.append(contentStr);qDebug() << str;m_tcpSocket81->write(str.toStdString().c_str()); }

?

?

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。