How to send HTTPHeader using QT WebEngine?

 ̄綄美尐妖づ 提交于 2019-12-14 03:57:32

问题


I was playing around with QT WebEngine to make a desktop web browser which suits my requirements while working. Unfortunately, I need send some data with the HTTP Header to a site. I came across QWebEngineUrlRequestInfo class' void QWebEngineUrlRequestInfo::setHttpHeader(const QByteArray &name, const QByteArray &value) method but I don't really know how to use it in code. This is my code so far:

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
        }
    }
}

Here is my main.cpp file:

#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();
}

Since the above code throws an error, I really can't figure out a fix for this problem. TIA


回答1:


This is my tiny project using webengine, FYI.

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


来源:https://stackoverflow.com/questions/38684517/how-to-send-httpheader-using-qt-webengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!