gpt4 book ai didi

c++ - Qt 5.7 QNetworkProxy 没有考虑cookies

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:39 24 4
gpt4 key购买 nike

我正在开发一个使用 QWebEngineView 和 QNetworkAccessManager 的 Qt 云客户端应用程序。我遇到的问题描述如下:

该应用程序有一个自定义登录表单。根据用户凭据,我执行一系列手动请求(“post”和“get”)以获取相关 session cookie。

    ...
//get session cookie
QNetworkAccessManager accessManager;
connect(&accessManager, &QNetworkAccessManager::proxyAuthenticationRequired, [=] (const QNetworkProxy &proxy, QAuthenticator *authenticator){
//perform proxy auth in case a proxy is set
});

QNetworkReply * reply = Q_NULLPTR;

QNetworkRequest request(QUrl(/*url*/));
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRawHeader("Cache-Control", "no-cache");

QByteArray data(/*data for auth*/);
reply = accessManager.post(request,data); //ajax login

QEventLoop waitReplyHandler;
QObject::connect(reply, SIGNAL(finished()), &waitReplyHandler, SLOT(quit()));

//wait for reply from url
waitReplyHandler.exec();
QVariant sessionCookie = reply->header(QNetworkRequest::SetCookieHeader); //this cookie is used to retrieve second "session cookie"
...

//Different function - get user session cookie
QNetworkRequest request(QUrl(/*url*/));

request.setHeader(QNetworkRequest::CookieHeader, sessionCookie);
QNetworkReply * reply = accessManager.get(request);

QEventLoop waitReplyHandler;
QObject::connect(reply, SIGNAL(finished()), &waitReplyHandler, SLOT(quit()));

//wait for reply from url
waitReplyHandler.exec();
QString redirect = reply->header(QNetworkRequest::LocationHeader).toString();
QVariant userSessionCookie = reply->header(QNetworkRequest::SetCookieHeader);

在此之后,我为 QWebEngineView 设置了一个请求拦截器:

    interceptor = new CWebEngineUrlRequestInterceptor(sessionCookie.value<QList<QNetworkCookie>>().first(),
userSessionCookie.value<QList<QNetworkCookie>>().first(),
this);
m_tabWebview->webEngineView()->page()->profile()->setRequestInterceptor(interceptor);
m_tabWebview->webEngineView()->load(redirectUrl); //Obtained from "redirect" string variable from second manual request

想法是在每次加载调用 Web 引擎 View 时使用这些手动获取的 cookie:

    void CWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
QByteArray sessionCookie = QByteArray(m_sessionCookie.name() + "=" + m_sessionCookie.value());
QByteArray userSessionCookie = QByteArray(m_userSessionCookie.name() + "=" + m_userSessionCookie.value());

QByteArray requestCookies = sessionCookie + "; " + userSessionCookie;

info.setHttpHeader(QByteArray("Cookie"), requestCookies);
}

这项工作非常正常,在加载到 redirectUrl 之后,Web View 将显示云的主页,用户已经登录。但是,如果我将 QNetworkProxy 设置为应用程序:

    QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName(proxyUrl);
proxy.setPort(proxyPort.toInt());

QNetworkProxy::setApplicationProxy(proxy);

上面的序列将在 web View 中显示我的目标 url 的登录页面。我相信应用程序级代理设置应该与 QWebEngineView 无关。手动请求有效,我能够获取 cookie,也调用了 interceptRequest,但代理中继后不保留 cookie。我还尝试手动设置 header 以代理自身:

    QByteArray requestCookies = m_cookies.first().name() + "=" + m_cookies.first().value() + "; " +
m_cookies.last().name() + "=" + m_cookies.last().value();

QNetworkProxy proxy(QNetworkProxy::applicationProxy());
proxy.setRawHeader(QByteArray("Cookie"), requestCookies);
QNetworkProxy::setApplicationProxy(proxy);
//...
//check headers are set to proxy
QByteArray cookieVar = QNetworkProxy::applicationProxy().rawHeader(QByteArray("Cookie"));
if(cookieVar.isEmpty()){
std::cout<<"empty cookies to proxy ";
}
else{
std::cout<<cookieVar.toStdString()<<std::endl;
}

日志没问题,我可以看到 cookie 已附加到代理。

如果没有互联网连接的时间超过 session cookie 的过期时间,则实现此自定义登录机制以处理自动登录。如何设置应用程序级代理以使用手动获取的 session cookie?

提前谢谢你。

最佳答案

这似乎是与代理身份验证机制相关的网络引擎框架中的错误。但是我找到了解决方法:当调用 QWebEnginePage::proxyAuthenticationRequired 时,在验证器对象的数据设置后,我只是再次调用加载到 requestUrl 参数。

connect(app->webview()->page(), &QWebEnginePage::proxyAuthenticationRequired, [=] (const QUrl &requestUrl, QAuthenticator *authenticator, const QString &proxyHost){
proxyAuthSequence(authenticator, proxyHost);
app->webview()->load(requestUrl);
});

想法是将初始加载视为虚拟加载,仅设置身份验证数据,然后再次加载 url。随后的加载调用将不再触发此信号。所以,基本上问题出在身份验证机制上。

该错误也已提交给 Qt: https://bugreports.qt.io/browse/QTBUG-58121

关于c++ - Qt 5.7 QNetworkProxy 没有考虑cookies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41505707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com