如何使用 QT WebEngine 发送 HTTPHeader?
我正在使用 QT WebEngine 来制作一个适合我工作时需求的桌面网络浏览器.不幸的是,我需要将一些带有 HTTP 标头的数据发送到站点.我遇到了 QWebEngineUrlRequestInfo 类' void QWebEngineUrlRequestInfo::setHttpHeader(const QByteArray&name, const QByteArray &value)
方法,但我真的不知道如何在代码中使用它.到目前为止,这是我的代码:
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
}
}
}
这是我的 main.cpp 文件:
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();
}
由于上面的代码抛出了一个错误,我实在想不出解决这个问题的办法.TIA
Since the above code throws an error, I really can't figure out a fix for this problem. TIA
推荐答案
这是我的小project 使用 webengine,仅供参考.
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");
}
相关文章