问题
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