How to login web-form using Qt5
-
What I'm doing is cli multiple IP checker, if its online login to its html form using: input type="text" name="username" id="username"; input type="password" name="password" id="password" and get value of few attribute but stuff like network.post(request, loginData) or similar which embedding username and password to the link somehow doesn't work, I need something like Mechanize approach, but ist only available in Ruby and Python. Any help appreciated, bunging my head on this few weeks, didn't get that done...
Update for the question:
I have wrote small multipart/form-data contains form itself and action file for testing the code below:
1. Form:
@<html> <head> <title>Test Page</title> </head> <body> <h2>Data Collection</h2><p> <form enctype="multipart/form-data" id="loginform" method="post" action="proceed.php"> <table> <tr> <td>Name:</td> <td><input type="text" name="username" id="username"/></td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password" id="password"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit"/> </td> </tr> </table> </form> </body> </html>@
2. Action file:
@<html> <body> Welcome <?php echo $_POST["username"]; ?><br> Your password is: <?php echo $_POST["password"]; ?> </body> </html>@
3. Code to test:
@ void MainWindow::login()
{
manager = new QNetworkAccessManager(this);//ssl errors ignoring connect(manager,SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this,SLOT(onIgnoreSSLErrors(QNetworkReply*,QList<QSslError>))); theurl = "http://localhost/form/"; QString userName = "usr"; QString userPassword = "pwd"; qDebug() << "Attempting to login with Username =" << userName << "and Password =" << userPassword; m_userName = userName; QHttpMultiPart *loginData = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart userNamePart; userNamePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("multipart/form-data; name=\"username\"; filename=\""+ userName + "\"")); userNamePart.setBody(userName.toUtf8()); QHttpPart userPasswordPart; userPasswordPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("multipart/form-data; name=\"password\"; filename=\""+ userPassword + "\"")); userPasswordPart.setBody(userPassword.toUtf8()); loginData->append(userNamePart); loginData->append(userPasswordPart); qDebug() << "OK with code1"; QNetworkRequest request(theurl); request.setRawHeader( "User-Agent" , "Mozilla Firefox" ); qDebug() << "OK with code2"; QNetworkReply *reply = manager->post( request, loginData); loginData->setParent(reply); qDebug() << "OK with code3"; connect( reply, SIGNAL( finished() ), SLOT( _q_onLoginRequestFinished() ) ); connect( reply, SIGNAL( metaDataChanged() ), SLOT( _q_onLoginMetaDataChanged() ) ); } void MainWindow::_q_onLoginRequestFinished() { qDebug() << Q_FUNC_INFO; QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() ); QByteArray ba = reply->readAll(); qDebug() << ba + " request-finished->"; if ( !reply ) return; QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(reply->header(QNetworkRequest::SetCookieHeader)); if(cookies.count() != 0){ //you must tell which cookie goes with which url manager->cookieJar()->setCookiesFromUrl(cookies, theurl); } qDebug() << QString::number(cookies.count()); if ( ba.isEmpty() ) { emit loggedInAs( m_userName ); m_isLoggedIn = true; emit isLoggedInChanged(); } } void MainWindow::_q_onLoginMetaDataChanged() { qDebug() << Q_FUNC_INFO; QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() ); if ( !reply ) return; QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); if ( !statusCode.isValid() ) return; int status = statusCode.toInt(); if ( status != 302 ) { QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); qDebug() << QString::number(status) + " status num" ; qDebug() << tr( "Loging request failed: %1." ).arg( reason ); emit loginFailed( reason ); } if(status == 302) { QUrl newUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); qDebug() << "redirected from xx to " + newUrl.toString(); QNetworkRequest newRequest(newUrl); manager->get(newRequest); return; } }@
This code only prints html form, but should submit and print action result instead.
What am I doing wrong? -
FUCK IT, I'M WRITING IT ON JAVA!