我目前正在尝试开发一个基本的浏览器网站,以便使用qt框架在特定的Internet站点上冲浪。我创建了一个继承QWebView的MyWindow类,以便处理在新浏览器窗口中可能出现的弹出窗口的打开。在这个MyWindow类中,我还重新创建了createWindow函数。此外,我还在MyWindow类中创建了QNetworkAccessManager和QNetworkCookieJar对象,而没有重新创建任何其他新函数。我认为这足以在我定位的网站上进行浏览,因为它的主页上有登录表单,而您可以仅使用服务器生成的cookie中包含的信息在同一网站上的其他页面上进行浏览。登录。它在“正常”导航期间效果很好,而在单击诸如
<a class="lien_default lienPerso" href="javascript:popupPerso('foo.php?login=bar')">bar</a>
在这种情况下,系统会提示一个新窗口(我发现javascript函数是一个简单的window.open),但似乎无法从cookie中检索信息:当前会话未使用,新窗口要求再次登录。只有在此弹出窗口中登录后,我才能浏览正确的链接页面。我的意图当然是使用每个当前会话的信息来访问此链接的信息。这种行为(即没有第二次登录请求)实际上是您使用标准浏览器浏览此网页时得到的。
我还发现由于JavaScript代码,linkClicked信号未处理这些链接。
请在我的代码下面找到:
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow mWind;
mWind.show();
return a.exec();
}
mywindow.cpp
#include <QWebView>
#include "mywindow.h"
MyWindow::MyWindow (QWidget * parent):QWebView(parent)
{
}
QWebView *MyWindow::createWindow(QWebPage::WebWindowType type)
{
Q_UNUSED(type);
QWebView *webView = new QWebView;
QWebPage *newWeb = new QWebPage(webView);
webView->setAttribute(Qt::WA_DeleteOnClose, true);
webView->setPage(newWeb);
return webView;
}
主窗口
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mywindow.h"
#include <QWebView>
#include <QWebFrame>
#include <QNetworkAccessManager>
#include <QNetworkCookieJar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QUrl url([url of the website I was targeting]);
ui->setupUi(this);
QWebSettings *settings = ui->w->settings(); // w is an object of MyWindow class that i promoted in Design Panel
settings->setAttribute(QWebSettings::JavascriptEnabled, true);
settings->setAttribute(QWebSettings::PluginsEnabled, true);
settings->setAttribute(QWebSettings::AutoLoadImages, true);
settings->setAttribute(QWebSettings::JavaEnabled, false);
settings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
ui->w->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks); // This was a test on links
QNetworkCookieJar* cookieJar = new QNetworkCookieJar();
QNetworkAccessManager* nam = new QNetworkAccessManager(this);
nam->setCookieJar(cookieJar);
ui->w->page()->setNetworkAccessManager(nam);
ui->w->load(url);
ui->w->show();
net=nam;
connect(ui->w,SIGNAL(linkClicked(QUrl)),this,SLOT(openUrl(QUrl)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openUrl(QUrl url)
{
QWebView *n = ui->w->createWindow(QWebPage::WebBrowserWindow);
n->page()->setNetworkAccessManager(net);
n->load(url);
n->show();
}
谢谢
希卡洛斯
最佳答案
在MyWindow::createWindow
中创建的页面似乎没有设置可从其获取cookie的networkAccessManager,请尝试以下操作:
QWebPage *newWeb = new QWebPage(webView);
newWeb->setNetworkAccessManager(page()->networkAccessManager());
另一种选择是创建QWebPage的子类,在其中您将执行此逻辑而不是在多个位置执行此逻辑,然后从createWindow返回此类对象。
关于javascript - Javascript不使用QWebView cookie/ session ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26325177/