Results 1 to 13 of 13

Thread: QNetworkAccessManager and post method

  1. #1
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Post post method

    my http connection is Working i send request to server through QNetworkAccessManager
    and also find the response i want to make post method ,i want to concatenate (password, username, address) with url please help how to make post method in Qt


    Thanks

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: post method

    I never tried this but, just to give to heads up check this out

    Qt Code:
    1. void QWebView::load ( const QNetworkRequest & request, QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, const QByteArray & body = QByteArray() )
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: post method

    You don't usually concatenate the parameters with the request URL in a POST call, that's a GET. If a GET is what you want the read the docs of QUrl as to how you use parameters ("query items").

    If you want a POST request call QNetworkAccessManager::post() with the data payload (i.e. the parameters) available via the second argument. The data payload depends on how your "form" is supposed to work. If you are mimicking a simple form then you should use "application/x-www-form-urlencoded" and encode the payload as if it were the query string for a GET request (QUrl can help here, but you only want the query string part). If you are mimicking a file upload then you need the more complicated "multipart/form-data".

  4. #4
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: post method

    Siri have been make connection and find response from the server but i want to bind a mobile number and password to the url and send to the server but i am not sucesses.please help me,pls see my code.
    .cpp

    Qt Code:
    1. #include <QNetworkAccessManager>
    2. #include <QUrl>
    3. #include <QNetworkRequest>
    4. #include <QNetworkReply>
    5. #include<stdlib.h>
    6.  
    7. Login::Login(QWidget *parent) :
    8. QDialog(parent),
    9. ui(new Ui::Login)
    10. {
    11. ui->setupUi(this);
    12.  
    13. }
    14.  
    15. Login::~Login()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void Login::on_pushButton_clicked()
    21. { int n;
    22.  
    23. QString str;
    24. QString str2;
    25.  
    26. str=ui->lineEdit->text();
    27. str2=ui->lineEdit_2->text();
    28.  
    29. n=str.count();
    30. if(n<10)
    31. {
    32. QMessageBox msgBox;
    33. msgBox.setText("Enter 10 digit Mobile number");
    34. msgBox.exec();
    35. }
    36. if(n>10)
    37. {
    38. QMessageBox msgBox;
    39. msgBox.setText("Enter 10 digit Mobile number");
    40. msgBox.exec();
    41. }
    42.  
    43. connection();
    44.  
    45. }
    46.  
    47. void Login::on_buttonBox_accepted()
    48. {
    49. MainWindow *mins = new MainWindow();
    50. mins->show();
    51. }
    52.  
    53. void Login::on_buttonBox_rejected()
    54. {
    55.  
    56. }
    57. void Login::connection()
    58. {
    59.  
    60. nam = new QNetworkAccessManager(this);
    61. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
    62. this, SLOT(finishedSlot(QNetworkReply*)));
    63.  
    64. QUrl url("http://qrrency.com/mobile/j2me/cab/Test.php?name=311");
    65. QNetworkReply *reply = nam->get(QNetworkRequest(url));
    66.  
    67. }
    68. void Login::finishedSlot(QNetworkReply* reply)
    69. {
    70. // Reading attributes of the reply
    71. QVariant statusCodeV =
    72. reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    73.  
    74. //int randomSayi = rand()%3;
    75.  
    76. QMessageBox msgBox;
    77.  
    78. QVariant tempQ = QVariant(statusCodeV.toString().length());
    79. msgBox.setText(tempQ.toString());
    80. int ret = msgBox.exec();
    81.  
    82. // "200 OK" received?
    83. if (statusCodeV.toInt()==200)
    84. {
    85. // TODO: read data from QNetworkReply
    86. msgBox.setText("Successfull!...");
    87. int ret = msgBox.exec();
    88.  
    89. QByteArray bytes = reply->readAll(); // bytes
    90. const QString string(bytes); // string
    91. //ui->textEdit->setText(string);
    92.  
    93. QMessageBox msgBox;
    94. msgBox.setText(string );
    95. msgBox.exec();
    96. }
    97. // Some http error or redirect
    98. else
    99. {
    100. // TODO:
    101. msgBox.setText("UnSuccessfull!...");
    102. int ret = msgBox.exec();
    103. }
    104.  
    105. delete reply;
    106.  
    107. }
    To copy to clipboard, switch view to plain text mode 

    when i take the value in first str and str2,then i call sever connection i want to pass this paramter to the function connection();
    -> send the data to the server
    Last edited by wysota; 3rd June 2011 at 19:03.

  5. #5
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: post method

    sorry to keep repeating this... CODE TAGS

  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: post method

    How does the server expect to receive the data?
    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
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: post method

    Quote Originally Posted by sabbu View Post
    Siri have been make connection and find response from the server but i want to bind a mobile number and password to the url and send to the server but i am not sucesses.please help me,pls see my code.
    Since your code at lines 64-65 makes absolutely no attempt to associate the content of the user's input with the URL or request I think there is very little we can do to help you until you understand what you are trying to do.

    Are you trying to do a GET request (in your code) or a POST request (in your original post)? Do you know which the server is expecting? Do you know the difference?
    Do you know the names of the parameters the server is expecting?

    when i take the value in first str and str2,then i call sever connection i want to pass this paramter to the function connection();
    Then declare connection() (line 57) to accept some parameters and pass them (line 43). This has nothing to do with a network GET/POST request - it is rudimentary programming.

    -> send the data to the server
    You cannot send the data to the server until you answer (and understand the answer) my earlier questions. We cannot help until you can express to us what you want to do and tried it yourself.


    Other observations:

    Your "validation" attempts at lines 29-41 are better enforced with a QRegExpValidator attached to the line edit.

    The code at lines 60-62 should be in the Login class constructor. As it is now, every time someone pushes the button a new QNetworkAccessManager is allocated on the heap and lost (memory leak). For every new there MUST eventually be a matching delete. If you allocate it on the heap in the constructor then you should put the matching delete in the destructor. BTW: It doesn't really need to be on the heap at all. Line 49 is similar memory leak.


    BTW: See how much easier it is for us to reference your code IF YOU BOTHER TO PUT THE CODE IN [code][/code] TAGS. You've been asked politely many times before, and Wysota has fixed it for you this time, but it is about time you played the game.

  8. #8
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Post QNetworkAccessManager and post method

    sir ,

    i have been make server connection through the QNetworkAccessManager but i want to make post method and send name, userid, and possword to server please help how to make post method in Qt.see my code
    Qt Code:
    1. nam = new QNetworkAccessManager(this);
    2. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
    3. this, SLOT(finishedSlot(QNetworkReply*)));
    4.  
    5. QUrl url(QLatin1String("http://taxinomics.com/mobile/Login.php?mobilenumber=number&password=m"));
    To copy to clipboard, switch view to plain text mode 
    this is get method i want to make post method how to make.i beginner for Qt.
    Thanks
    Last edited by wysota; 13th June 2011 at 00:34. Reason: missing [code] tags

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager and post method

    What was wrong with your earlier thread? You start these threads and as soon as it becomes apparent that no-one is going to post the exact, fully working solution within a day or so you just drop the thread only to come back later with the same question.

    To convert a GET to a POST you take the parameters you were encoding into the URL and put them in the POST request content with the relevant headers. The previous thread gives you all the components to achieve this.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  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: QNetworkAccessManager and post method

    Threads merged. Please don't multipost ever again.
    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 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Post post method

    my http connction is working but when i send a mobile number and password exact he give response -1 means your mobile number is not match.please any one help me where i am worng my code is below.
    Qt Code:
    1. Login::Login(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Login)
    4. {
    5. ui->setupUi(this);
    6.  
    7. }
    8.  
    9. Login::~Login()
    10. {
    11. delete ui;
    12. }
    13.  
    14. void Login::on_pushButton_clicked()
    15. {
    16.  
    17. QString str;
    18. QString str2;
    19.  
    20. str=ui->lineEdit->text();
    21. str2=ui->lineEdit_2->text();
    22.  
    23. connection(str,str2 );
    24.  
    25. }
    26.  
    27.  
    28.  
    29. void Login::connection( QString str,QString str2)
    30. {
    31.  
    32. QByteArray postData;
    33. postData.append("mobilenumber=");
    34. postData.append(str);
    35. postData.append("&password=");
    36. postData.append(str2);
    37. nam = new QNetworkAccessManager(this);
    38. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
    39. this, SLOT(finishedSlot(QNetworkReply*)));
    40.  
    41. QUrl url(QLatin1String("http://taxinomics.com/mobile/Login.php? "));
    42.  
    43. QNetworkReply *reply = nam->post(QNetworkRequest(url),postData);
    44.  
    45.  
    46.  
    47. }
    48. void Login::finishedSlot(QNetworkReply* reply)
    49. {
    50.  
    51. QVariant statusCodeV =
    52. reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    53.  
    54. QMessageBox msgBox;
    55. QVariant tempQ = QVariant(statusCodeV.toString().length());
    56. msgBox.setText(tempQ.toString());
    57. int ret = msgBox.exec();
    58.  
    59. // "200 OK" received?
    60. if (statusCodeV.toInt()==200)
    61. {
    62.  
    63. msgBox.setText("Successfull!...");
    64. int ret = msgBox.exec();
    65.  
    66. QByteArray bytes = reply->readAll(); // bytes
    67. const QString string(bytes); // string
    68.  
    69. QMessageBox msgBox;
    70. msgBox.setText(string );
    71. msgBox.exec();
    72. }
    73.  
    74. else
    75. {
    76.  
    77. msgBox.setText("UnSuccessfull!...");
    78. int ret = msgBox.exec();
    79. }
    80.  
    81. delete reply;
    82.  
    83. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 16th June 2011 at 14:52. Reason: missing [code] tags

  12. #12
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: post method

    removed contents
    Last edited by schnitzel; 16th June 2011 at 22:47.

  13. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: post method

    To fix your "-1" response I suggest you send a valid mobilenumber and password or fix the logic of your web application. That is the response I get with a rubbish mobilenumber and password using wget (i.e. nothing to do with Qt):
    Qt Code:
    1. $ wget -S --post-data='mobilenumber=0123456789&password=rubbish' http://taxinomics.com/mobile/Login.php
    2. --2011-06-17 07:58:25-- http://taxinomics.com/mobile/Login.php
    3. Resolving taxinomics.com (taxinomics.com)... 173.205.127.22
    4. Connecting to taxinomics.com (taxinomics.com)|173.205.127.22|:80... connected.
    5. HTTP request sent, awaiting response...
    6. HTTP/1.1 200 OK
    7. Date: Thu, 16 Jun 2011 21:58:25 GMT
    8. Server: Apache
    9. Expires: Thu, 19 Nov 1981 08:52:00 GMT
    10. Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    11. Pragma: no-cache
    12. Set-Cookie: PHPSESSID=cc6f1b1c2580586dda06ca681a5988af; path=/
    13. Cache-Control: public
    14. Connection: close
    15. Content-Type: text/html; charset=utf-8
    16. Length: unspecified [text/html]
    17. Saving to: `Login.php'
    18.  
    19. [ <=> ] 2 --.-K/s in 0s
    20.  
    21. 2011-06-17 07:58:26 (684 KB/s) - `Login.php' saved [2]
    22. $ cat Login.php
    23. -1
    To copy to clipboard, switch view to plain text mode 


    You don't need the '?' and trailing space at the end of the URL: your URL is "http://taxinomics.com/mobile/Login.php?%20" (although this does not seem to be breaking it).
    You can use QUrl to build the payload with due care for encoding of values. What happens at the moment if the password contains an '&'?

Similar Threads

  1. HTTP Post from QNetworkAccessManager - no data sent
    By secureboot in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2011, 18:46
  2. POST and QNetworkAccessManager
    By hakermania in forum Newbie
    Replies: 1
    Last Post: 13th February 2011, 00:05
  3. Replies: 0
    Last Post: 21st December 2009, 05:04
  4. QNetworkAccessManager::post() never returns
    By danc81 in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2009, 09:13
  5. QNetworkAccessManager double post
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2009, 04:44

Tags for this Thread

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.