Using QWebEnginePage to download web pages. Works very well but sometimes it doesn't work with certain URLs and appears to be a header issue. I can't figure out what headers to send to not get this error.
Header:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QApplication>
#include <QWebEnginePage>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QWebEnginePage *p;
Ui::MainWindow *ui;
protected slots:
void getHtml(bool s);
void textChanged();
signals:
};
#endif // MAINWINDOW_H
Source
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QWebEnginePage>
#include <QWebEngineSettings>
#include <QWebEngineHttpRequest>
QWebEngineHttpRequest httpR;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
p = new QWebEnginePage(this);
httpR.setHeader("Location", "absoluteURI");
httpR.setUrl(QUrl("https://ca.finance.yahoo.com/quote/AIPT/history?period1=1238597365&period2=1554130165&interval=1d&filter=history&frequency=1d"));
p->settings()->setAttribute(QWebEngineSettings::AutoLoadImages, false);
p->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
connect(p, SIGNAL(loadFinished(bool)), this, SLOT(getHtml(bool)));
p->load(httpR);
}
QString html = "";
void MainWindow::getHtml(bool s){
p->toHtml(
[this](QString result) {
html=result;
this->textChanged();
qDebug()<<httpR.headers();
});
}
void MainWindow::textChanged(){
qDebug()<<html.size();
}
MainWindow::~MainWindow(){
delete ui;
}
Error message:
js: Unrecognized Content-Security-Policy directive 'disown-opener'.
js: Unrecognized Content-Security-Policy directive 'disown-opener'.
860801 QVector("Location") js: Unrecognized Content-Security-Policy directive 'disown-opener'.
js: Unrecognized Content-Security-Policy directive 'disown-opener'.
js: Unrecognized Content-Security-Policy directive 'disown-opener'.
js: The resource https://pagead2.googlesyndication.com/pagead/js/r20190327/r20190131/show_ads_impl.js was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it Please make sure it has an appropriate as value and it is preloaded intentionally.
So I finally figured it out. It appears to fix the "Unrecognized Content-Security-Policy directive 'disown-opener'" error you would have to incorporate the html tag attribute "rel=noopener" in each link tag. However since I want to download html webpages from other sources it would be a bit hard to change each link.
Instead after some trial and error I found that this header "Upgrade: websocket" which fixed the issue.