#include <QDebug>#include <QRegExp>#include "qrequester.h"QRequester::QRequester(QObject *parent) : QThread(parent){ m_netManager = new QNetworkAccessManager(); QReNetworkCookieJar *cookieJar = new QReNetworkCookieJar(this); m_netManager->setCookieJar(cookieJar); connect(m_netManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));}QRequester::~QRequester(){}QNetworkRequest QRequester::makeRequest(QUrl _url){ QNetworkRequest request(_url); request.setRawHeader("Host", "mobile.twitter.com"); request.setRawHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; ru; rv:1.9.0.10) Gecko/2009042809 GranParadiso/3.0.10"); request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); request.setRawHeader("Accept-Language", "ru,en-us;q=0.7,en;q=0.3"); //request.setRawHeader("Accept-Encoding", "gzip,deflate"); request.setRawHeader("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7"); request.setRawHeader("Keep-Alive", "300"); request.setRawHeader("Connection", "keep-alive"); return request;}void QRequester::DoRequest(){ QNetworkRequest request = makeRequest(QUrl("https://mobile.twitter.com/session/new")); QNetworkReply *reply = m_netManager->get(request); //reply->ignoreSslErrors(); m_step = STEP_AUTHPAGE;}void QRequester::replyFinished(QNetworkReply* _reply){ qDebug() << "Reply Finished!"; switch (m_step){ case STEP_AUTHPAGE:{ QString html = QString(_reply->readAll()); // Пробуем достать урл логина и токен QRegExp rxAction = QRegExp("action=\"([a-zA-Z0-9\.\-\:\/]+)\"", Qt::CaseInsensitive); rxAction.indexIn(html); QString loginURL = rxAction.cap(1); QRegExp rxToken = QRegExp("input name=\"authenticity_token\" type=\"hidden\" value=\"([a-zA-Z0-9]+)\"", Qt::CaseInsensitive); rxToken.indexIn(html); QString token = rxToken.cap(1); qDebug() << "Login URL: "+loginURL; qDebug() << "Login Token: "+token+"="+token.toLocal8Bit(); QNetworkRequest request = makeRequest(QUrl(loginURL)); QUrl postData; QReNetworkCookieJar *cookie = qobject_cast<QReNetworkCookieJar*>(_reply->manager()->cookieJar()); QList<QNetworkCookie> cookieList = cookie->getAllCookies(); //qvariant_cast<QList<QNetworkCookie> >(_reply->header(QNetworkRequest::SetCookieHeader).toList()); qDebug() << " ----- Cookies ----- "; request.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(cookieList)); request.setRawHeader("authenticity_token", token.toLocal8Bit()); request.setRawHeader("username", "*****"); request.setRawHeader("password", "*****"); QNetworkReply *reply = m_netManager->post(request, postData.encodedQuery()); m_step = STEP_HOMEPAGE; } break; case STEP_HOMEPAGE:{ QString html = QString(_reply->readAll()); qDebug() << html; } break; }}
C++ (Qt)QUrl postData; QReNetworkCookieJar *cookie = qobject_cast<QReNetworkCookieJar*>(_reply->manager()->cookieJar());QList<QNetworkCookie> cookieList = cookie->getAllCookies(); //qvariant_cast<QList<QNetworkCookie> >(_reply->header(QNetworkRequest::SetCookieHeader).toList()); qDebug() << " ----- Cookies ----- ";request.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(cookieList)); request.setRawHeader("authenticity_token", token.toLocal8Bit());request.setRawHeader("username", "*****");request.setRawHeader("password", "*****"); QNetworkReply *reply = m_netManager->post(request, postData.encodedQuery());
QNetworkReply *reply = m_netManager->post(request, "");
<html><body>You are being <a href="http://mobile.twitter.com/">redirected</a>.</body></html>