Results 1 to 13 of 13

Thread: QHttp Post work inkorect

  1. #1
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default QHttp Post work inkorect

    Hi i have problem with POST request.

    I make this:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. http = new QHttp(this);
    11.  
    12. connect(http, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
    13. connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));
    14. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(sendGet()));
    15.  
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void MainWindow::sendGet()
    24. {
    25. http->setHost("ts9.travian.pl");
    26. http->post("/dorf1.php", "name=Gaths&password=qwerasdf&lowRes=1&login=1359299878&w=");
    27. //http->get("/dorf1.php");
    28.  
    29. }
    30.  
    31. void MainWindow::stateChanged(int state)
    32. {
    33. switch(state)
    34. {
    35. case 0 :
    36. ui->labelState->setText("Unconnected");
    37. break;
    38. case 1 :
    39. ui->labelState->setText("Host LookUP");
    40. break;
    41. case 2 :
    42. ui->labelState->setText("Connectig");
    43. break;
    44. case 3 :
    45. ui->labelState->setText("Sending");
    46. break;
    47. case 4 :
    48. ui->labelState->setText("Reding");
    49. break;
    50. case 5 :
    51. ui->labelState->setText("Connect");
    52. break;
    53. case 6 :
    54. ui->labelState->setText("Closing");
    55. break;
    56. }
    57. }
    58.  
    59. void MainWindow::requestFinished(int id, bool error)
    60. {
    61. ui->plainTextEdit->appendPlainText(http->readAll());
    62. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    http://pastebin.com/A5ehkvRu

    In curl work correctyl
    Qt Code:
    1. curl -d "name=Gaths&password=qwerasdf&lowRes=1&login=1359299878&w=" ts9.travian.pl/dorf1.php
    To copy to clipboard, switch view to plain text mode 
    Result:
    http://pastebin.com/zB0UY0T4



    Can someone tell what i do wrong in qt ??

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp Post work inkorect

    Are we supposed to diff those two files ourselves to see the difference between them?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Post work inkorect

    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 ??
    Last edited by Pablik; 28th January 2013 at 19:14.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp Post work inkorect

    We have no idea what you mean by that it doesn't work correctly. What is correct in this case?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Post work inkorect

    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).

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp Post work inkorect

    And what kind of help do you expect from us? Apparently the two requests differ so take a network sniffer and find the difference.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Post work inkorect

    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=13592 99878&w=") maybe in Qt is different way to do this

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp Post work inkorect

    Get a network sniffer such as wireshark and find out differences between the two requests.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Post work inkorect

    but i know, that parametrs are good and should work "name=Gaths&password=qwerasdf&lowRes=1&login=1 3592 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)

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp Post work inkorect

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Post work inkorect

    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%3A60 0&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)

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHttp Post work inkorect

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Post work inkorect

    i change QHttp to QNetworkAccessManager and set base64 but still dont work ;(

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. networkManager = new QNetworkAccessManager(this);
    11.  
    12. connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
    13. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(send()));
    14.  
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void MainWindow::send()
    23. {
    24. QByteArray postData;
    25. postData.append("name=Gaths&");
    26. postData.append("password=qwerasdf&");
    27. postData.append("s1=Login&");
    28. postData.append("w=1024%3A600&");
    29. postData.append("login=1359443665");
    30.  
    31. networkManager->post(QNetworkRequest(QUrl("http://ts9.travian.pl/dorf1.php")), postData.toBase64());
    32.  
    33.  
    34. }
    35.  
    36.  
    37. void MainWindow::requestFinished(QNetworkReply *reply)
    38. {
    39.  
    40. if(reply->isOpen())
    41. {
    42. ui->plainTextEdit->setPlainText(reply->readAll());
    43. reply->close();
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

    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!!!!!

    Qt Code:
    1. void MainWindow::send()
    2. {
    3.  
    4. QUrl postData;
    5. postData.addQueryItem("name", "Gaths");
    6. postData.addQueryItem("password","qwerasdf");
    7. postData.addQueryItem("s1","Login");
    8. postData.addQueryItem("w","1024%3A600");
    9. postData.addQueryItem("login","1359443665");
    10.  
    11.  
    12. QNetworkRequest request(QUrl("http://ts9.travian.pl/dorf1.php"));
    13. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    14. networkManager->post(request, postData.encodedQuery());
    15.  
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    THX for help
    Last edited by Pablik; 29th January 2013 at 12:19.

Similar Threads

  1. Qhttp:Post() - multipart/form-data
    By arunredi in forum Newbie
    Replies: 5
    Last Post: 20th September 2011, 20:02
  2. QHTTP POST problem
    By oscar in forum Qt Programming
    Replies: 5
    Last Post: 10th October 2009, 19:58
  3. problems in QHttp post method!
    By Longe.W in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2009, 05:21
  4. QHttp::post() - cannot upload file
    By arunredi in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2008, 12:13
  5. QHttp::post() help
    By Funklord in forum Qt Programming
    Replies: 4
    Last Post: 22nd March 2006, 07:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.