PDA

View Full Version : QNetworkAccessManager: problem to post a form after the login.



cydside
30th August 2009, 15:18
Hi to all,
I'm trying to post a form after the login on the web(a classic login.php page), so I've divided the tasks into two step(two methods im my class) and I've ran those sequential(Login and Post) but I've had at first step a correct feedback instead on posting the form I've recieved an error like not logged in type. So, I've joined the tasks into a single method but it doesn't work yet. Maybe the code will explain better:



void MainWindow::postData()
{
QString url;
QString host;
QByteArray username("username=myname&password=mypassword");

userAgentS = "My App";
encodingS = "utf-8";
charCrLf = "\r\n";

url = "http://www.xyz.com/login.php";
refererS = "http://www.xyz.com/login_page.php";

host = url.right(url.length() - url.indexOf("://") - 3);
host = host.left(host.indexOf("/"));

QNetworkAccessManager * http = new QNetworkAccessManager(this);
connect(http, SIGNAL(finished(QNetworkReply *)), this,
SLOT(readData(QNetworkReply *)));

QNetworkRequest request;
request.setRawHeader("Host", host.toAscii());
request.setRawHeader("User-Agent", userAgentS.toAscii());
request.setRawHeader("Referer", refererS.toAscii());

QString contentType = "application/x-www-form-urlencoded";

request.setHeader(QNetworkRequest::ContentTypeHead er, contentType.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHe ader,
QVariant(username.size()).toString());
request.setUrl(QUrl(url));

http->post(request, username);

/*-------------------------------------------------------------------------*/
/* Posting the Form */

addField("project_id","1");
addField("category","GUI");
addField("summary","Prova");
addField("description","Indovina che Bug è?");

refererS = "http://www.xyz.com/report_page.php";
url = "http://www.xyz.com/report.php";
host = url.right(url.length() - url.indexOf("://") - 3);
host = host.left(host.indexOf("/"));

qsrand(QDateTime::currentDateTime().toTime_t());
QString b = QVariant(qrand()).toString() + QVariant(qrand()).toString()
+ QVariant(qrand()).toString();
QString boundary = "---------------------------"+b;
QString endBoundary = charCrLf + "--" + boundary + "--" + charCrLf;
contentType = "multipart/form-data; boundary=" + boundary;
boundary = "--" + boundary + charCrLf;
QByteArray bond = boundary.toAscii();
QByteArray send;
bool first = true;

for (int i=0; i < fieldNames.size(); i++)
{
send.append(bond);
if (first)
{
boundary = charCrLf + boundary;
bond = boundary.toAscii();
first = false;
}

send.append(QString("Content-Disposition: form-data; name=\"" +
fieldNames.at(i) + "\"" + charCrLf).toAscii());

if (encodingS == "utf-8")
{
// send.append(QString("Content-Transfer-Encoding: 8bit" +
// charCrLf).toAscii());
send.append(charCrLf.toAscii());
send.append(strToEnc(fieldValues.at(i)));
}
}

send.append(endBoundary.toAscii());

request.setRawHeader("Host", host.toAscii());
request.setRawHeader("User-Agent", userAgentS.toAscii());
request.setRawHeader("Referer", refererS.toAscii());

request.setHeader(QNetworkRequest::ContentTypeHead er, contentType.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHe ader,
QVariant(send.size()).toString());
request.setUrl(QUrl(url));

http->post(request, send);
}

void MainWindow::readData(QNetworkReply * r)
{
data = r->readAll();
ui->textEdit->append(QString(data).toUtf8());
qDebug() << "readData eror(): " << r->error();
}


:crying:
Thanks!