.pro
C++ (Qt)
QT += core gui network webkit
.h
C++ (Qt)
#include <QtWebKit/QWebPage>
#include <QtWebKit/QWebFrame>
#include <QtWebKit/QWebElement>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
.cpp
C++ (Qt)
// evaluateJavaScript
QString login = ""; // логин от форума
QString password = ""; // пароль от форума
QWebPage page;
QNetworkCookieJar *cookieJar = new QNetworkCookieJar;
page.networkAccessManager()->setCookieJar(cookieJar);
page.mainFrame()->load(QUrl("http://www.prog.org.ru/"));
QEventLoop loop;
connect(&page, SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
loop.exec();
QString curSessionId = page.mainFrame()->toHtml();
curSessionId.replace(QRegExp("^.+hashLoginPassword\\(this, '([^']+)'\\);.+"), "\\1");
QString postUrl = page.mainFrame()->toHtml();
postUrl.replace(QRegExp("^.+<form action=\"([^\"]+)\".+$"), "\\1");
QWebElement user = page.mainFrame()->findFirstElement("input[name=user]");
QWebElement passwd = page.mainFrame()->findFirstElement("input[name=passwrd]");
user.setAttribute("value", login);
passwd.setAttribute("value", password);
page.mainFrame()->evaluateJavaScript("hashLoginPassword(document.forms[0], '" + curSessionId + "');");
QWebElement hash_passwrd_we = page.mainFrame()->findFirstElement("input[name=hash_passwrd]");
QString hash_passwrd = hash_passwrd_we.attribute("value");
// POST REQUEST
QNetworkAccessManager qnam;
qnam.setCookieJar(cookieJar);
QNetworkRequest request;
request.setUrl(QUrl(postUrl));
QByteArray postData = "user=" + login.toUtf8() + "&passwrd=&cookielength=-1&hash_passwrd=" + hash_passwrd.toUtf8();
QNetworkReply *reply = qnam.post(request, postData);
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if(reply->error() != QNetworkReply::NoError) {
qDebug() << reply->errorString() << reply->error();
}
request.setUrl(QUrl(reply->rawHeader("Refresh").replace("0; URL=", "")));
reply = qnam.get(request);
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if(reply->error() != QNetworkReply::NoError) {
qDebug() << reply->errorString() << reply->error();
}
// qDebug() << endl << "Request headers: ";
// QList<QByteArray> reqHeaders = reply->request().rawHeaderList();
// foreach (QByteArray reqName, reqHeaders)
// {
// QByteArray reqValue = reply->request().rawHeader(reqName);
// qDebug() << reqName << ": " << reqValue;
// }
// qDebug() << "Reply headers: ";
// QList<QByteArray> reqHeadersReply = reply->rawHeaderList();
// foreach( QByteArray reqName, reqHeadersReply )
// {
// QByteArray reqValue = reply->rawHeader( reqName );
// qDebug() << reqName << ": " << reqValue;
// }
QFile file(QDir::currentPath() + "/test.html");
file.open(QIODevice::WriteOnly);
file.write(reply->readAll());
file.close();
qDebug() << "finish!";
delete reply;
delete cookieJar;