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

时间:2024-04-09 18:01:29

目录

 

背景

关键

演示及源码


 

背景

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

 

演示及源码

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

上面的script就是向127.0.0.1:81端口的数据:

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

请求头如下:

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

程序结构如下:

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

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QTcpServer;
class QTcpSocket;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    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 msg
    QString 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 msg
    QString 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());
}