Read http header in qt webengine
-
Hello,
I want to capture server http response in qt demo browser and check the header fields. How to implement this using qt webengine?
I also saw QWebEngineUrlRequestInterceptor but using this class we cant read http header fields.
I want to read http headers in qt source code. For example, i want to add a method in demo browser that print http headers of all requests. There is no need to capture traffic or any tools as dev tools or wireshark.
Thanks -
Hi,
As far as i know Chrome implements its own network stack not accessible from Qt.
Anyway, if your only goal is to read http headers, QNetworkManager allows to do that pretty easely.
Connect the QNetworkReply to metaDataChanged() signal, that's all.
The headers are accessible through the headers/rawheaders methods of the reply.
See Qt docs for more infos. -
Thanks for your answer,but i want read http header when a page is loaded into webengine view and qnerwork is not accessible.
@Arhk said in Read http header in qt webengine:
and qnerwork is not accessible.
Why not ?
I've done a download manager based on QNetworkManager on a QWebEngine app, i'm using meta infos as filename, file length etc, and no issues so far.Except doing the request yourself, I have no clue.
It's not a big deal, only a few lines of code is needed.
Why not have a try ? -
it,s correct but i want to add a method into demo browser project to print http headers. In demo browser webenginepage is used to load page and i cant use qnetworkmanager.
Can you give me some codes which implement my method using qnetwork in demo browser project. -
Looking at the demobrowser sources, it turns out it's pretty easy since demobrowser use a QNetworkManager itself !
In the load(url) method of WebView.cpp do:
void WebView::loadUrl(const QUrl &url) { m_initialUrl = url; load(url); QNetworkReply *reply = BrowserApplication::networkAccessManager()->get(QNetworkRequest(url)); QObject::connect(reply, &QNetworkReply::metaDataChanged, [reply] () { qDebug()<<reply->url(); qDebug()<<reply->rawHeaderPairs(); reply->abort(); reply->deleteLater(); } ); }
-
thanks for your reply but I dont want to create a new request to just get header field. I want to get it directly from qwebengineurlrequestinfo object in Qwebenineurlrequestinceptor class. I need to use the http headers and take a decision to set another header field.