How to send QString to server in Qt 5.6?



  • Before, when I was using Qt 5.5, I could send network requests this way:

    QString jsonString = "Some string info";
    uploadManager = new QNetworkAccessManager(this);
    QNetworkRequest rqData (theApp->getDomain() + "PageOnServerName.php"); 
    rqData.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); 
    QNetworkReply * r = uploadManager->post(rqData, jsonString.toLatin1());
    QEventLoop loop; 
    QTimer::singleShot(std::max(10u,timeout_msecs),&loop,SLOT(quit())); 
    r->connect(r, SIGNAL(finished()), &loop, SLOT(quit())); 
    r->connect(r, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit())); 
    loop.exec();
    

    But right now it doesn't seem to work anymore. Server doesn't get any information


  • Lifetime Qt Champion

    Hi,

    You should check whether you get any error from the query.


  • Moderators

    any warnings in the console?
    What is the server address? In case it's a https-conenction check if your applciation can find the OpenSSL libraries.



  • @raven-worx turns out there's problem with cookies for some reason. uploadManager contains different cookies. For example cookies should be "PHPSESSION = 12345", but uploadManager has "PHPSESSION = 67890". That's why it says "Invalid session" everytime. How do I set right cookies to uploadManager? I've tried uploadManager->cookieJar()->cookiesForUrl(QUrl("http://page.com/PageOnServerName.php")); and it still gives me wrong cookies. Any suggestions?


  • Moderators

    @d1.psy
    make sure you reuse the same QNetworkAccessManager instance. When the server sends you a cookie, the QNAM stores it automatically and reuses them in the next request.



  • @raven-worx in QT 5.5 I had this function

    QNetworkAccessManager *MainWindow::manager ()  {
        if (getView()) {
            if (getView()->page()) {
                QNetworkAccessManager *p =  getView()->page()->networkAccessManager();
    			return p;
            }
        }
    

    And manager was used to send post requests to server. It had right cookies

    QNetworkReply * r = manager()->post(rqData, jsonString.toLatin1());
    

    but since QWebEngine doesn't with QNAM, I'm using this

    QNetworkAccessManager *MainWindow::manager ()  {
        if (getView()) {
            if (getView()->page()) {
    			return uploadManager;
            }
        }
    
    QNetworkReply * r = uploadManager->post(rqData, jsonString.toLatin1());
    

    and uploadManager is using wrong cookies


  • Moderators

    @d1.psy
    so you want the cookies from the requests you already made with QtWebEngine?
    If so take a look at QWebEngineCookieStore (see it's signals) and "sync" the cookies into your QNAM.



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