void MainWindow::postData()
{
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::ContentTypeHeader, contentType.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHeader,
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 boundary
= "---------------------------"+b;
QString endBoundary
= charCrLf
+ "--" + boundary
+ "--" + charCrLf;
contentType = "multipart/form-data; boundary=" + boundary;
boundary = "--" + boundary + charCrLf;
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::ContentTypeHeader, contentType.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHeader,
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();
}
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::ContentTypeHeader, contentType.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHeader,
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::ContentTypeHeader, contentType.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHeader,
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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks