Results 1 to 3 of 3

Thread: Post some data onto the local server by QNetworkAccessManager(Qt5)

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Post some data onto the local server by QNetworkAccessManager(Qt5)

    Trying to post the paramaters onto the server but have no avail(following is my folder)

    My file resides(my local host == C:\wamp\www)
    https://farm4.staticflickr.com/3544/...ff858f14_c.jpg

    bridge.html
    Qt Code:
    1. <html>
    2. <form action="general_query.php" method="post">
    3. <input type="text" name="db_server">
    4. <input type="text" name="db_user">
    5. <input type="text" name="db_password">
    6. <input type="text" name="db_database">
    7. <input type="text" name="query_message">
    8. </form>
    9. </html>
    To copy to clipboard, switch view to plain text mode 

    general_query.php
    Qt Code:
    1. <?php
    2.  
    3. $response = array();
    4. $response["server"] = _$POST['db_server'];
    5. $response["user"] = _$POST['db_user'];
    6. $response["password"] = _$POST['db_password'];
    7. $response["db"] = _$POST['db_database'];
    8. $response["query_message"] = _$POST['query_message'];
    9. echo($response);
    10. ?>
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "MainWindow.hpp"
    2. #include <QApplication>
    3.  
    4. #include <QDebug>
    5. #include <QByteArray>
    6. #include <QEventLoop>
    7. #include <QNetworkReply>
    8. #include <QTextCodec>
    9. #include <QUrl>
    10. #include <QUrlQuery>
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14.  
    15. QNetworkRequest request("http://127.0.0.1/bridge/bridge.html");
    16. request.setHeader(QNetworkRequest::ContentTypeHeader,
    17. "application/x-www-form-urlencoded");
    18.  
    19. QUrlQuery query;
    20. query.addQueryItem("db_database", "a");
    21. query.addQueryItem("db_password", "b");
    22. query.addQueryItem("db_server", "c");
    23. query.addQueryItem("db_user", "d");
    24. query.addQueryItem("query_message", "e");
    25.  
    26. QUrl post_data;
    27. post_data.setQuery(query);
    28.  
    29. QNetworkAccessManager network_manager;
    30. auto network_reply = network_manager.post(request,
    31. query.query().toUtf8());
    32.  
    33. QEventLoop loop;
    34. connect(network_reply, SIGNAL(finished()), &loop, SLOT(quit()));
    35. loop.exec();
    36.  
    37. QByteArray raw_data;
    38. if(network_reply->error() == QNetworkReply::NoError){
    39. raw_data = network_reply->readAll();
    40. }else{
    41. qDebug()<<network_reply->errorString();
    42. }
    43.  
    44. qDebug()<<QTextCodec::codecForHtml(raw_data)->toUnicode(raw_data);
    45.  
    46. }
    To copy to clipboard, switch view to plain text mode 

    The results are same as the texts of the bridge.html but not the json as I expected

    expected results
    Qt Code:
    1. {
    2. "server": "c"
    3. "user": "d"
    4. "password": "b"
    5. "db": "a"
    6. "query message": "e"
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by stereoMatching; 9th March 2014 at 12:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Post some data onto the local server by QNetworkAccessManager(Qt5)

    Shouldn't your query URL be the PHP script?

    I.e. instead of
    Qt Code:
    1. QNetworkRequest request("http://127.0.0.1/bridge/bridge.html");
    To copy to clipboard, switch view to plain text mode 
    using
    Qt Code:
    1. QNetworkRequest request("http://127.0.0.1/bridge/general_query.php");
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    stereoMatching (10th March 2014)

  4. #3
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Post some data onto the local server by QNetworkAccessManager(Qt5)

    Quote Originally Posted by anda_skoa View Post
    Shouldn't your query URL be the PHP script?

    I.e. instead of
    Qt Code:
    1. QNetworkRequest request("http://127.0.0.1/bridge/bridge.html");
    To copy to clipboard, switch view to plain text mode 
    using
    Qt Code:
    1. QNetworkRequest request("http://127.0.0.1/bridge/general_query.php");
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Thank you very much, I find out my errors.

    first error, wrong php script
    Qt Code:
    1. <?php
    2.  
    3. $response["response"] = array();
    4.  
    5. $result = array();
    6. $result["server"] = $_POST['db_server'];
    7. $result["user"] = $_POST['db_user'];
    8. $result["password"] = $_POST['db_password'];
    9. $result["database"] = $_POST['db_database'];
    10. $result["message"] = $_POST['query_message'];
    11.  
    12. array_push($response["response"], $result);
    13.  
    14. echo json_encode($response);
    15. ?>
    To copy to clipboard, switch view to plain text mode 

    second, the request url is incorrect

    Qt Code:
    1. QNetworkRequest request("http://127.0.0.1/bridge/general_query.php");
    To copy to clipboard, switch view to plain text mode 

    third, I don't need QUrl, QUrlQuery is enough for the task
    Last edited by stereoMatching; 10th March 2014 at 00:45.

Similar Threads

  1. Again QNetworkAccessManager POST and PHP
    By ZSWASW in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2014, 19:38
  2. QNetworkAccessManager post
    By januszmk in forum Newbie
    Replies: 2
    Last Post: 13th April 2012, 09:01
  3. HTTP Post from QNetworkAccessManager - no data sent
    By secureboot in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2011, 18:46
  4. POST and QNetworkAccessManager
    By hakermania in forum Newbie
    Replies: 1
    Last Post: 13th February 2011, 00:05
  5. QNetworkAccessManager double post
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2009, 04:44

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.