PDA

View Full Version : QHttp Post work inkorect



Pablik
27th January 2013, 17:03
Hi i have problem with POST request.

I make this:


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

http = new QHttp(this);

connect(http, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(sendGet()));

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::sendGet()
{
http->setHost("ts9.travian.pl");
http->post("/dorf1.php", "name=Gaths&password=qwerasdf&lowRes=1&login=1359299878&w=");
//http->get("/dorf1.php");

}

void MainWindow::stateChanged(int state)
{
switch(state)
{
case 0 :
ui->labelState->setText("Unconnected");
break;
case 1 :
ui->labelState->setText("Host LookUP");
break;
case 2 :
ui->labelState->setText("Connectig");
break;
case 3 :
ui->labelState->setText("Sending");
break;
case 4 :
ui->labelState->setText("Reding");
break;
case 5 :
ui->labelState->setText("Connect");
break;
case 6 :
ui->labelState->setText("Closing");
break;
}
}

void MainWindow::requestFinished(int id, bool error)
{
ui->plainTextEdit->appendPlainText(http->readAll());
}



Result:
http://pastebin.com/A5ehkvRu

In curl work correctyl


curl -d "name=Gaths&password=qwerasdf&lowRes=1&login=1359299878&w=" ts9.travian.pl/dorf1.php

Result:
http://pastebin.com/zB0UY0T4



Can someone tell what i do wrong in qt ??

wysota
28th January 2013, 15:22
Are we supposed to diff those two files ourselves to see the difference between them?

Pablik
28th January 2013, 19:55
Head is the same , but body is different . Look on line cout
But the content is not important. Why this post request doesn't work correctly ??

wysota
28th January 2013, 20:31
We have no idea what you mean by that it doesn't work correctly. What is correct in this case?

Pablik
28th January 2013, 21:09
Post request is the same in Qt and in curl, but correct result is only in curl. Qt give wrong result, like paramtrs ware wrong (but i use the same parametrs in both cases).

wysota
28th January 2013, 22:45
And what kind of help do you expect from us? Apparently the two requests differ so take a network sniffer and find the difference.

Pablik
28th January 2013, 23:25
I want from you to show me how correct configure POST request in QT, because now is somting wrong , the same result i get if i use GET request. Curl show correct result (using this parametrs "name=Gaths&password=qwerasdf&lowRes=1&login=1359299878&w=") maybe in Qt is different way to do this

wysota
28th January 2013, 23:39
Get a network sniffer such as wireshark and find out differences between the two requests.

Pablik
28th January 2013, 23:47
but i know, that parametrs are good and should work "name=Gaths&password=qwerasdf&lowRes=1&login=13592 99878&w="
name=Gaths
password=qwerasdf
lowRes=1
login=13592 99878
w=

And work, but in qt dont work :( (i do something wrong but i dont know what)

wysota
29th January 2013, 00:04
Get a network sniffer and find out differences between two requests. You are comparing unencoded body of the request which says nothing about what gets sent to the server. Don't guess what gets sent, check it using tools available. You will see many differences between the two requests, you need to find those that matter (e.g. content encoding header) and correct your request. Your "problem" comes from your apparent limited knowledge about HTTP so either you learn how HTTP works or you mimic a working request. The latter is certainly faster than the former, however you learn much more from the former than the latter.

Pablik
29th January 2013, 09:08
thx for help (i use wireshark), i know what is wrong, it is problem with encoding.

POST parametrs from firefox IN WIRESHARK look like this:
"name=Gaths&password=qwerasdf&s1=Login&w=1024%3A600&login=1359443665"

but from qt POST look like this:
"6e616d234567843234356445678956774343..."

Can somebody tell me how correctli encode parametrs in qt ?? (my app code is in first post)

wysota
29th January 2013, 11:46
You need to provide proper encoding for the data.

http://en.wikipedia.org/wiki/POST_(HTTP)

It should probably be x-www-form-urlencoded but it looks like base64 now.

And by the way, I suggest you use QNetworkAccessManager rather than QHttp which has been deprecated for years.

Pablik
29th January 2013, 13:19
i change QHttp to QNetworkAccessManager and set base64 but still dont work ;(



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

networkManager = new QNetworkAccessManager(this);

connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(send()));

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::send()
{
QByteArray postData;
postData.append("name=Gaths&");
postData.append("password=qwerasdf&");
postData.append("s1=Login&");
postData.append("w=1024%3A600&");
postData.append("login=1359443665");

networkManager->post(QNetworkRequest(QUrl("http://ts9.travian.pl/dorf1.php")), postData.toBase64());


}


void MainWindow::requestFinished(QNetworkReply *reply)
{

if(reply->isOpen())
{
ui->plainTextEdit->setPlainText(reply->readAll());
reply->close();
}
}


If i set postData.toBase64() or just postData, result its the same , in wireshark write "Media Type: application/octet-stream 67 bytes", but should by "Line-based text data: application/x-www-form-urlencoded" .

How i can convert postData to x-www-form-urlencoded.

WORKKKKK!!!!!



void MainWindow::send()
{

QUrl postData;
postData.addQueryItem("name", "Gaths");
postData.addQueryItem("password","qwerasdf");
postData.addQueryItem("s1","Login");
postData.addQueryItem("w","1024%3A600");
postData.addQueryItem("login","1359443665");


QNetworkRequest request(QUrl("http://ts9.travian.pl/dorf1.php"));
request.setHeader(QNetworkRequest::ContentTypeHead er, "application/x-www-form-urlencoded");
networkManager->post(request, postData.encodedQuery());


}


THX for help