c++ - 如何使用 QT WebEngine 发送 HTTPHeader?

标签 c++ qt qml qtwebengine webengine

我正在使用 QT WebEngine 来制作一个适合我工作时要求的桌面 Web 浏览器。不幸的是,我需要将一些带有 HTTP header 的数据发送到站点。我遇到了QWebEngineUrlRequestInfo class' void QWebEngineUrlRequestInfo::setHttpHeader(const QByteArray &name, const QByteArray &value) 方法,但我真的不知道如何在代码中使用它。到目前为止,这是我的代码:

import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtWebEngine 1.3

ApplicationWindow {
    visible: true
    width: 320
    height: 240
    title: qsTr("Browser")
    flags: Qt.WindowStaysOnTopHint

    WebEngineView {
        anchors.fill: parent
        url: "some http url here"
        Rectangle{
            height: 100
            width: height
        }
    }
}

这是我的 main.cpp 文件:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtWebEngine/qtwebengineglobal.h>
#include <QWebEngineUrlRequestInfo>
#include <QByteArray>

int main(int argc, char *argv[])
{
    QByteArray key, value;
    key.append("SomeKey");
    value.append("SomeValue");
    QWebEngineUrlRequestInfo url;//Won't work because its constructor is private
    url.setHttpHeader(key, value);
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QtWebEngine::initialize();
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));

    return app.exec();
}

由于上面的代码抛出了一个错误,我真的想不出解决这个问题的方法。时间差

最佳答案

这是我的小 project使用网络引擎,仅供引用。

view->page()->profile()->setRequestInterceptor(/*instance inerit QWebEngineUrlRequestInterceptor*/)
...
void /*class inerit WebEngineUrlRequestInterceptor*/::interceptRequest(QWebEngineUrlRequestInfo &info)
{
    info.setHttpHeader("Accept-Language", "zh-CN,zh;q=0.8");
}

关于c++ - 如何使用 QT WebEngine 发送 HTTPHeader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684517/

相关文章:

c++ - 使用 C++ 访问 Qt/QML 对象

c++ - 如何在qml中动态地向模型添加数据?

c++ - 将消息注入(inject) WPF 应用程序消息泵

c++ - 加密编译可执行文件中的常量字符串(例如密码)

c++ - QObject::connect 未将信号连接到插槽

c++ - Qt:如何将基类传输到该类中创建的对象

c++ - 使用 QNetworkAccessManager 在 QT 中的 HTTP POST 请求

c++ - Qt Qvariantlist转换成javascript数组不成功

c++ - 从静态函数创建类对象

c++ - QtCreator,子项目链接器错误