Thanks. Though the problem remains, that with the first query i receive the correct answer from Youtube, but with the 2nd i return to the consent page, which demands correct cookie. In the code below i achieve right answers all the time but only when i delete QNetworkCookieJar each time, which is not the right practice according the documentation, and that also may lead to runtime error.
{
manager = new QNetworkAccessManager(this);
cookieJar = new QNetworkCookieJar(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotReadyRead(QNetworkReply*)));
}
void Youtube
::makeRequest(QString indexCore
) {
QNetworkRequest request;
QNetworkCookie cookie;
cookie.setName("CONSENT=YES+42");
cookieJar->insertCookie(cookie);
manager->setCookieJar(cookieJar);
std::cout << termcolor::yellow << "YOUTUBE::makeRequest()" << termcolor::reset<< std::endl;
request.setRawHeader("COOKIE" , "CONSENT=YES+42" ); //works
request.
setUrl(QUrl("https://" + indexCore
));
manager->get(request);
}
void Youtube::slotReadyRead(QNetworkReply *replay)
{
website = dataTemp.toStdString();
manager->cookieJar()->deleteCookie(cookie);
std::cout << termcolor::yellow << "Youtube::slotReadyRead(QNetworkReply *replay)" << termcolor::reset<< std::endl;
replay->deleteLater();
manager->autoDeleteReplies();
manager->clearAccessCache();
manager->clearConnectionCache();
manager->setAutoDeleteReplies(true);
}
Youtube::Youtube(QObject *parent) : QObject(parent)
{
manager = new QNetworkAccessManager(this);
cookieJar = new QNetworkCookieJar(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotReadyRead(QNetworkReply*)));
}
void Youtube::makeRequest(QString indexCore)
{
QNetworkRequest request;
QNetworkCookie cookie;
cookie.setName("CONSENT=YES+42");
cookieJar->insertCookie(cookie);
manager->setCookieJar(cookieJar);
std::cout << termcolor::yellow << "YOUTUBE::makeRequest()" << termcolor::reset<< std::endl;
request.setRawHeader("COOKIE" , "CONSENT=YES+42" ); //works
request.setUrl(QUrl("https://" + indexCore ));
manager->get(request);
}
void Youtube::slotReadyRead(QNetworkReply *replay)
{
QByteArray dataTemp = replay->readAll();
website = dataTemp.toStdString();
manager->cookieJar()->deleteCookie(cookie);
std::cout << termcolor::yellow << "Youtube::slotReadyRead(QNetworkReply *replay)" << termcolor::reset<< std::endl;
replay->deleteLater();
manager->autoDeleteReplies();
manager->clearAccessCache();
manager->clearConnectionCache();
manager->setAutoDeleteReplies(true);
}
To copy to clipboard, switch view to plain text mode
Bookmarks