Login to a website
-
Hey everyone.
I'm gonna create a new program that can login to a website with keeping cookies and sessions. Please help me how can I do that ??!!!Thanks !!
-
Use i.e. QNetworkAccessManager class. Prepare request with loging URL, authentication, etc. and use either GET or POST to send to the WWW "login request".
This all depends what authentication mechanism WWW that You want login into uses. -
We have to keep the cookies !!!
How to do that ??!! -
Hi,
@AliReza-Beytari said:
We have to keep the cookies !!!
How to do that ??!!Search Google for "Qt cookies"
-
QNetworkAccessManager + QNetworkCookieJar = QNetworkCookieJar * QNetworkAccessManager::cookieJar() const
http://doc.qt.io/qt-5/qnetworkaccessmanager.html#cookieJar
You can write application that will store (when app is closed), reuse, delete and edit cookies if You like. -
I use it like this :
void MainWindow::on_pushButton_clicked()
{
// http://test.com/login.php --------> Login page
// http://test.com/usercp.php --------> User control page [that need the cookies]QUrl serviceUrl = QUrl("http://test.com/login.php"); QNetworkAccessManager *networkManager = new QNetworkAccessManager(this); connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*))); QUrlQuery postData; postData.addQueryItem("username", "my_username"); postData.addQueryItem("password", "my_password"); QNetworkRequest request(serviceUrl); request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); networkManager->post(request, postData.toString(QUrl::FullyEncoded).toUtf8()); QNetworkCookieJar *cookies = networkManager->cookieJar(); QEventLoop eventLoop; QNetworkAccessManager mgr; mgr.setCookieJar(cookies); QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit())); QNetworkRequest req(QUrl("http://test.com/usercp.php")); QNetworkReply *reply = mgr.get(req); eventLoop.exec(); if (reply->error() == QNetworkReply::NoError) { QMessageBox::information(this, "Result", QString(reply->readAll())); }
}
void MainWindow::serviceRequestFinished(QNetworkReply *reply)
{
QMessageBox::information(this, "Result", QString(reply->readAll()));
}But I couldn't send cookies !!!
Please help !!! -
Nothing ?!!
-
@AliReza-Beytari:
Your main problem is: You did not wait for login.php to finish replying, before you sent your request to usercp.php. -
So, how can I wait for login.php to finish replying ??!!
-
@AliReza-Beytari said:
So, how can I wait for login.php to finish replying ??!!
Wait for the
QNetworkAccessManager::finished()
orQNetworkReply::finished()
signals. These signals are emitted when the reply has finished.Do you know how to use signals and slots? http://doc.qt.io/qt-5/signalsandslots.html