如何从Qt QWebEngineView获取HTTP状态代码



我一直在查看QT文档,以找到在加载页面并发出请求后从QWebEngineView返回http状态代码的方法。我知道QWebEngineView不与QNetWorkAccessManager交互,这将使实现这一点变得轻而易举。那么,有没有其他方法可以使用QWebEngineView从请求中获取HTTP状态代码??我尝试了多种方法,包括RequestInterceptor。下面是我尝试使用RequestInterceptor的一个片段,但它并没有给我实际的HTTP状态代码,只是常数值,这是我不想要的。

/*************requestinterceptor.h**************/
#ifndef REQUESTINTERCEPTOR_H
#define REQUESTINTERCEPTOR_H
#include <QWebEngineUrlRequestInterceptor>
#include <QWebEngineUrlRequestInfo>
#include <QDebug>
#include <QUrl>
class RequestInterceptor : public QWebEngineUrlRequestInterceptor
{
public:
explicit RequestInterceptor(QObject * parent = Q_NULLPTR);
virtual void interceptRequest(QWebEngineUrlRequestInfo & info) Q_DECL_OVERRIDE;
};
#endif // REQUESTINTERCEPTOR_H
/*************requestinterceptor.cpp***************/
#include "requestinterceptor.h"
RequestInterceptor::RequestInterceptor(QObject * parent)
:QWebEngineUrlRequestInterceptor(parent){}
void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo & info)
{
QString rsrct = "";
switch(info.resourceType())
{
case 1:rsrct="ResourceTypeSubFrame, --> frame or iframe";break;
case 2:rsrct="ResourceTypeStylesheet, --> a CSS stylesheet";break;
case 3:rsrct="ResourceTypeScript, --> an external script";break;
case 4:rsrct="ResourceTypeImage, --> an image (jpg/gif/png/etc)";break;
case 5:rsrct="ResourceTypeFontResource, --> a font";break;
case 6:rsrct="ResourceTypeSubResource, --> an other subresource.";break;
case 7:rsrct="ResourceTypeObject, --> an object (or embed) tag for a plugin,";break;
case 8:rsrct="ResourceTypeMedia, --> a media resource.";break;
case 9:rsrct="ResourceTypeWorker, --> the main resource of a dedicated worker.";break;
case 10:rsrct="ResourceTypeSharedWorker, --> the main resource of a shared worker.";break;
case 11:rsrct="ResourceTypePrefetch, --> an explicitly requested prefetch";break;
case 12:rsrct="ResourceTypeFavicon, --> a favicon";break;
case 13:rsrct="ResourceTypeXhr, --> a XMLHttpRequest";break;
case 14:rsrct="ResourceTypePing, --> a ping request for <a ping>";break;
case 15:rsrct="ResourceTypeServiceWorker, --> the main resource of a service worker.";break;
case 16:rsrct="ResourceTypeUnknown";break;
default : rsrct="Unknown type";break;
}
qDebug()<<"t"<<Q_FUNC_INFO<<":ntt" << 
"WebUrlRequestInterceptor::interceptRequest    " <<info.requestMethod()
<<"rn  "<<info.requestUrl()<<"   "<<rsrct      <<"rn";
}

/*************mainwindow.cpp***************/
const QUrl url("http://google.com");
view = new WebView();
view = new QWebEngineView();
interceptor = new RequestInterceptor(view);
profile = new QWebEngineProfile(view);
profile->setRequestInterceptor(interceptor);
page = new QWebEnginePage(profile,view);
view->setPage(page);

经过数小时的搜索,我发现目前无法通过QWebEngine获取HTTP状态代码。加载完成事件不返回任何状态代码以及

然后我找到了的解决方案

setting = webpage.settings()
setting.setAttribute(QWebEngineSettings.ErrorPageEnabled, False)

相关内容

  • 没有找到相关文章

最新更新