C++ QNetworkReply和301重定向
我有一个webviewer,只希望它能够访问我们的webapps,为了实现这一点,我在Qt应用程序中放置了一个php头。这很好,但有一个例外,那就是301永久移动状态代码。现代浏览器会自动为您重定向,但会在http请求的末尾添加“/” 当我们的web应用的URL被输入时,它当前需要尾部斜杠才能检测到标题,但我希望它也能检测到该标题,即使他们没有放置尾部斜杠 以下是我当前检索标题的方法:C++ QNetworkReply和301重定向,c++,qt,qnetworkaccessmanager,C++,Qt,Qnetworkaccessmanager,我有一个webviewer,只希望它能够访问我们的webapps,为了实现这一点,我在Qt应用程序中放置了一个php头。这很好,但有一个例外,那就是301永久移动状态代码。现代浏览器会自动为您重定向,但会在http请求的末尾添加“/” 当我们的web应用的URL被输入时,它当前需要尾部斜杠才能检测到标题,但我希望它也能检测到该标题,即使他们没有放置尾部斜杠 以下是我当前检索标题的方法: QNetworkAccessManager *manager = new QNetworkAccessMana
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setUrl(url);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
request.setRawHeader("User-Agent", "CytoViewer 1.0");
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/CytoViewer");
QNetworkReply *reply = manager->get(request);
reply->ignoreSslErrors();
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
qDebug() << "QLoop reply all: " << reply->readAll();
qDebug() << "QLoop: " << reply->rawHeader("Cyto-Study-Manager");
if(reply->rawHeader("OurWebApp") == "1"){
//Header exists?(QEventLoop finish) Set arg[1]"url 'Found prouct: product header'"
product = reply->rawHeader("Product");
return true;
} else {
//Header doen't exist? Graceful error - not a valid PI product
return false;
}
长话短说,我正在发送两个网络请求,1)检查301 2)检查我们的应用程序头
在一个请求中是否有这样做的方法?我是否缺少自动执行此重定向的方法
问候
内森显然没有 有一个官方的HOWTO条目(不幸的是,死链接) 从上面的链接中提取:
void QNAMRedirect::replyFinished(QNetworkReply* reply) {
/*
* Reply is finished!
* We'll ask for the reply about the Redirection attribute
* http://doc.trolltech.com/qnetworkrequest.html#Attribute-enum
*/
QVariant possibleRedirectUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
/* We'll deduct if the redirection is valid in the redirectUrl function */
_urlRedirectedTo = this->redirectUrl(possibleRedirectUrl.toUrl(),
_urlRedirectedTo);
/* If the URL is not empty, we're being redirected. */
if(!_urlRedirectedTo.isEmpty()) {
QString text = QString("QNAMRedirect::replyFinished: Redirected to ")
.append(_urlRedirectedTo.toString());
this->_textContainer->setText(text);
/* We'll do another request to the redirection url. */
this->_qnam->get(QNetworkRequest(_urlRedirectedTo));
}
else {
/*
* We weren't redirected anymore
* so we arrived to the final destination...
*/
QString text = QString("QNAMRedirect::replyFinished: Arrived to ")
.append(reply->url().toString());
this->_textContainer->setText(text);
/* ...so this can be cleared. */
_urlRedirectedTo.clear();
}
/* Clean up. */
reply->deleteLater();
}
QUrl QNAMRedirect::redirectUrl(const QUrl& possibleRedirectUrl,
const QUrl& oldRedirectUrl) const {
QUrl redirectUrl;
/*
* Check if the URL is empty and
* that we aren't being fooled into a infinite redirect loop.
* We could also keep track of how many redirects we have been to
* and set a limit to it, but we'll leave that to you.
*/
if(!possibleRedirectUrl.isEmpty() &&
possibleRedirectUrl != oldRedirectUrl) {
redirectUrl = possibleRedirectUrl;
}
return redirectUrl;
}
如果您不使用Qt 6,请使用旧答案:
默认情况下不启用:
QNetworkRequest req(QUrl("https://example.com/"));
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
我想可能没有办法,但不确定我是否遗漏了什么,谢谢你清理了@kfunk:)官方指南的链接被破坏了:(@DavidSánchez:更新了评论以反映这一点。不幸的是,维基内容在删除之前没有被移动:(你说旧是什么意思?对我来说它是新的。感谢你提到这一点,你保存了我的a**,我将从5.5更新到5.6以使其生效。此处快速更新:
QNetworkRequest::FollowRedirectsAttribute
已被弃用,取而代之的是QNetworkRequest::RedirectPolicyAttribute
(检查)。
QNetworkRequest req(QUrl("https://example.com/"));
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);