Login and save session to navigate to other pages
Unsolved
General and Desktop
-
Greetings,
I have implemented QAccessNetworkManager and using cookie however the output is just the html content of login page. My purpose is to login then save the session to navigate to other page. I have also implemented to retry if "reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()" not equal to 200. However, it's not work as even though the page display Access Denied content, the url did not navigate so it's stay 200 which indicate success.
Below is my current code:
#include <QSslConfiguration> #include <QNetworkCookieJar> #include <QNetworkCookie> #include <QUrlQuery> #include <QDataStream> Downloader::Downloader(QObject *parent) { } void Downloader::doDownload() { QSslConfiguration sslConf = QSslConfiguration::defaultConfiguration(); sslConf.setPeerVerifyMode(QSslSocket::VerifyNone); QSslConfiguration::setDefaultConfiguration(sslConf); mManager = new QNetworkAccessManager(this); mManager->setCookieJar(new QNetworkCookieJar(this)); connect(mManager,SIGNAL(finished(QNetworkReply*)), this,SLOT(replyFinished(QNetworkReply*))); //Cookies// QFile f("C:/test/cookie.txt"); f.open(QIODevice::ReadOnly); QDataStream s(&f); while(!s.atEnd()){ QByteArray c; s >> c; QList<QNetworkCookie> list = QNetworkCookie::parseCookies(c); qDebug() << "eee" << list; jar = new QNetworkCookieJar(); jar->insertCookie(list.at(0)); } QString username = "backup"; QString password = "backup"; QUrlQuery postData; postData.addQueryItem("userName=",username); postData.addQueryItem("password=",password); qDebug() << postData.toString(QUrl::FullyEncoded).toUtf8(); QUrl URL("https://127.0.0.1/ui/backup/files?3-1.ILinkListener-files-2-download"); //QUrl URL("https://127.0.0.1/ui/login"); URL.setQuery(postData); QNetworkRequest QNR(URL); QNR.setHeader(QNetworkRequest::ContentTypeHeader, "text/html;charset=UTF-8"); mManager->post(QNR,postData.toString(QUrl::FullyEncoded).toUtf8()); } void Downloader::replyFinished(QNetworkReply *reply) { if(reply->error()) { qDebug() << "ERROR!"; qDebug() << reply->errorString(); qDebug()<<"SSL version string: "<<QSslSocket::sslLibraryVersionString(); qDebug()<<"SSL version use for run-time: "<<QSslSocket::sslLibraryVersionNumber(); } else { qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString(); qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();; qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong(); qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString(); QList<QNetworkCookie> list = mManager->cookieJar()->cookiesForUrl(QUrl("https://192.168.20.167/ui/backup/files?3-1.ILinkListener-files-2-download")); QFile f("C:/test/cookie.txt"); f.open(QIODevice::ReadWrite); for(int i = 0; i < list.size(); ++i){ QDataStream s(&f); s << list.at(i).toRawForm(); } f.close(); QFile *file = new QFile("C:/test/downloaded.txt"); if(file->open(QFile::Append)) { file->write(reply->readAll()); file->flush(); file->close(); } delete file; } reply->deleteLater(); }
Could someone guide or give some hint what I must do to login, save session and redirect to page with the saved session?
Thank you so much for your help and time.