我正在尝试使用QWebEngineProfile,QWebEnginePage和QWebEngineCookieStore使用QWebEngineView打开一个jsp文件。我可以为从应用程序发起的呼叫设置Cookie。但是随后会启动多个jsp调用,但未为该调用设置cookie。 代码截图如下。
QWebEngineSettings* webEngineSettings = QWebEngineSettings::defaultSettings(); webEngineSettings->setAttribute(QWebEngineSettings::WebAttribute::AutoLoadImages, true); webEngineSettings->setAttribute(QWebEngineSettings::WebAttribute::JavascriptEnabled, true); webEngineSettings->setAttribute(QWebEngineSettings::WebAttribute::LocalStorageEnabled, true);
QWebEngineProfile* profile = new QWebEngineProfile("abc", parent);
profile->setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies);
profile->setHttpCacheType(QWebEngineProfile::HttpCacheType::MemoryHttpCache);
profile->setHttpCacheMaximumSize(10);
QWebEngineCookieStore* cookieStore = profile->cookieStore();
cookieStore->loadAllCookies();
QNetworkCookie* webEngineNewCookie = new NetworkCookie(QByteArray("MyCookie"), QByteArray("1234"));
QUrl url = "https://example.com/help.jsp"
cookieStore->setCookie(*webEngineNewCookie, QUrl(url));
QStringList location = QStandardPaths::standardLocations(QStandardPaths::CacheLocation);
profile->setPersistentStoragePath(location.at(0));
QWebEnginePage* page = new QWebEnginePage(profile,parent);
page->action(QWebEnginePage::WebAction::InspectElement);
QWebEngineHttpRequest* webEngineHttpReq = new QWebEngineHttpRequest(QUrl(url), QWebEngineHttpRequest::Post);
parent->setPage(page);
parent->load(*webEngineHttpReq);
当我进行此调用时,我确实在此(https://example.com/help.jsp)中获得了cookie,但是此后从help.jsp启动了另一个调用,这些调用可能没有cookie,因此它们被服务器。