Login to a website
-
wrote on 30 May 2015, 11:16 last edited by
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 !!
-
wrote on 30 May 2015, 13:24 last edited by
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. -
wrote on 30 May 2015, 14:44 last edited by
We have to keep the cookies !!!
How to do that ??!! -
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"
-
wrote on 31 May 2015, 09:35 last edited by
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. -
wrote on 3 Jun 2015, 13:13 last edited by AliReza Beytari 6 Jun 2015, 12:36
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 !!! -
wrote on 6 Jun 2015, 12:35 last edited by
Nothing ?!!
-
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 !!!@AliReza-Beytari:
Your main problem is: You did not wait for login.php to finish replying, before you sent your request to usercp.php. -
wrote on 8 Jun 2015, 09:08 last edited by
So, how can I wait for login.php to finish replying ??!!
-
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
1/10