[Solved]Post some data onto the local server by QNetworkAccessManager(Qt5)



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

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

    bridge.html
    @
    <html>
    <form action="general_query.php" method="post">
    <input type="text" name="db_server">
    <input type="text" name="db_user">
    <input type="text" name="db_password">
    <input type="text" name="db_database">
    <input type="text" name="query_message">
    </form>
    </html>
    @

    general_query.php
    @
    <?php

    $response = array();
    $response["server"] = _$POST['db_server'];
    $response["user"] = _$POST['db_user'];
    $response["password"] = _$POST['db_password'];
    $response["db"] = _$POST['db_database'];
    $response["query_message"] = _$POST['query_message'];
    echo($response);
    ?>
    @

    main.cpp
    @
    #include "MainWindow.hpp"
    #include <QApplication>

    #include <QDebug>
    #include <QByteArray>
    #include <QEventLoop>
    #include <QNetworkReply>
    #include <QTextCodec>
    #include <QUrl>
    #include <QUrlQuery>

    int main(int argc, char *argv[])
    {

    QNetworkRequest request("http://127.0.0.1/bridge/bridge.html");
    request.setHeader(QNetworkRequest::ContentTypeHeader,
    "application/x-www-form-urlencoded");

    QUrlQuery query;
    query.addQueryItem("db_database", "a");
    query.addQueryItem("db_password", "b");
    query.addQueryItem("db_server", "c");
    query.addQueryItem("db_user", "d");
    query.addQueryItem("query_message", "e");
    
    QUrl post_data;
    post_data.setQuery(query);
    QNetworkAccessManager network_manager;
    auto network_reply = network_manager.post(request,
                                               query.query().toUtf8());
    
    QEventLoop loop;
    connect(network_reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec&#40;&#41;;
    
    QByteArray raw_data;
    if(network_reply->error(&#41; == QNetworkReply::NoError&#41;{
         raw_data = network_reply->readAll(&#41;;
    }else{
        qDebug(&#41;<<network_reply->errorString();
    }
    
    qDebug()<<QTextCodec::codecForHtml(raw_data)->toUnicode(raw_data);
    

    }
    @

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

    expected results
    @
    {
    "server": "c"
    "user": "d"
    "password": "b"
    "db": "a"
    "query message": "e"
    }
    @



  • Thanks for Qt Centre, I find out my errors.

    first error, wrong php script
    @
    <?php

    $response["response"] = array();

    $result = array();
    $result["server"] = $_POST['db_server'];
    $result["user"] = $_POST['db_user'];
    $result["password"] = $_POST['db_password'];
    $result["database"] = $_POST['db_database'];
    $result["message"] = $_POST['query_message'];

    array_push($response["response"], $result);

    echo json_encode($response);
    ?>
    @

    second, the request url is incorrect

    @
    QNetworkRequest request("http://127.0.0.1/bridge/general_query.php");
    @

    third, I don't need QUrl, QUrlQuery is enough for the task


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.