我用QtWebEngine创建了Qt浏览器。而且它不想通过代理解析dns-requests。
QWebEngineSettings::DnsPrefetchEnabled
不工作。
我试着通过 QWebEngineUrlRequestInterceptor
也没有用。
一个例子。
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
};
#endif // MAINWINDOW_H
主窗口.h
#include "mainwindow.h"
#include <QDebug>
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QNetworkProxy>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName("proxy_host");
proxy.setPort(proxy_port);
proxy.setCapabilities(QNetworkProxy::HostNameLookupCapability);
//proxy.setUser(user);
//proxy.setPassword(pass);
QNetworkProxy::setApplicationProxy(proxy);
QWebEngineProfile *profile = QWebEngineProfile::defaultProfile();
QWebEngineSettings* sett = profile->settings();
sett->setAttribute(QWebEngineSettings::PluginsEnabled, true);
sett->setAttribute(QWebEngineSettings::XSSAuditingEnabled, false);
sett->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
sett->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
sett->setAttribute(QWebEngineSettings::DnsPrefetchEnabled, false);
profile->setUseForGlobalCertificateVerification();
QWebEngineView *w = new QWebEngineView(this);
QWebEnginePage *p = new QWebEnginePage(profile,this);
w->setPage(p);
w->load(QUrl("https://browserleaks.com/ip"));
this->setCentralWidget(w);
this->resize(900,900);
}
我写了一个本地代理服务器,并使用 --host-resolver-rules
标志(5.15时终于开始工作了)把所有的东西都引用到它上面。任务大致解决了,但如果你知道更优雅的解决方案,欢迎来到我的小屋。