How to send Cookie request headers in Qt?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
16 views
mainwindow.h
private: QList cookies;

in the constructor:
cookie.setName("visible"); cookie.setValue("0"); cookie.setDomain("site.ru"); cookie.setPath("/dev/mobile"); cookie.setExpirationDate(QDateTime().addYears(9999)); cookies << cookie;

method that sends the request with the cookies:
void MainWindow::sendRequest(const QString& urlString){ log("sendRequest"); QTimer timer; timer.setInterval(3000); timer.setSingleShot(true); QEventLoop loop; currentUrlString = urlString; url->setUrl(urlString); request->setUrl(*url); request->setHeader(QNetworkRequest::CookieHeader, qVariantFromValue(cookies)); qDebug() << "Cookie:" << request->header(QNetworkRequest::CookieHeader).toList(); QNetworkReply* reply = manager->get(*request); QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); QObject::connect(&timer, &QTimer::timeout, reply, &QNetworkReply::abort); timer.start(); loop.exec(); }

When check,
qDebug() << "Cookie:" << request->header(QNetworkRequest::CookieHeader).toList();

the list is empty, it turns out that cookies are not sent
by | 16 views

1 Answer

0 like 0 dislike
Look closely, that returns a QVariant::toList.
You need to convert explicitly:
request->header(QNetworkRequest::CookieHeader).value>()


PS
This design is outdated: qVariantFromValue. Use QVariant::fromValue().
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked May 3, 2019 by Tesla4o
110,608 questions
257,186 answers
0 comments
35,430 users