Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAccessManager and xml reply
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager and xml reply

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    niteshnarayanlal
    wrote on last edited by
    #1

    hi I was trying to send a get request to a controller which responds through xml . I am using QXmlStreamReader to read the reply my problem is when I open the url in my browser then I am getting the appropriate reply but the same is not true with my code
    here is my code
    @
    void ServiceProvider::replyFinished(QNetworkReply * reply)
    {
    qDebug()<<reply->readAll();
    QString data=(QString)reply->readAll();
    qDebug()<<data;
    QXmlStreamReader xml(data);
    QXmlStreamAttributes attrib;
    while(!xml.atEnd())
    {
    xml.readNextStartElement();
    if(xml.name()=="name" && !xml.isEndElement())
    {
    attrib=xml.attributes();
    QString name=attrib.value("name").toString();
    qDebug()<<name;
    }
    }
    }

    void ServiceProvider::pushImage()
    {
    manager = new QNetworkAccessManager(this);
    manager->get(QNetworkRequest(QUrl("https://localhost/conductor/api/provider_accounts.xml")));
    connect(manager, SIGNAL(finished(QNetworkReply*)),
    this, SLOT(replyFinished(QNetworkReply*)));

    }

    @

    [edit: please wrap your code in @-tags, Eddy]

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      What do your two qDebugs() show?

      Honesly, though, I'm unfamiliar with what happens if you call readAll twice on a QIODevice. I always assumed that the first readAll() "ate" all of the data and would prevent a second one from working properly.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        niteshnarayanlal
        wrote on last edited by
        #3

        thanks for replying , but both of them are not showing anything . I am getting a blank reply .

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          Check the error() method in your network reply before you try to read anything. See if there is an error being set.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            niteshnarayanlal
            wrote on last edited by
            #5

            there was a problem as in I need to be logged in before making a request to that url for eg. when I do the same process with my browser then I open the login page and enter with my proper credentials and then if I make the request then I get the desired output . So I tried the following code .

            @
            void ServiceProvider::replyFinished(QNetworkReply * reply)
            {
            if(reply->error()==QNetworkReply::NoError)
            {
            qDebug()<<"No error";
            }
            else
            qDebug()<<"error";
            QString data=(QString)reply->readAll();
            qDebug()<<data;
            QXmlStreamReader xml(data);
            QXmlStreamAttributes attrib;
            while(!xml.atEnd())
            {
            xml.readNextStartElement();
            if(xml.name()=="name" && !xml.isEndElement())
            {
            attrib=xml.attributes();
            QString name=attrib.value("name").toString();
            qDebug()<<name;
            }
            }
            }

            void ServiceProvider::provideAuthenication(QNetworkReply reply, QAuthenticatorauthenticator)
            {
            Login *ob=new Login();

            authenticator->setUser(ob->userName);
            authenticator->setPassword(ob->password);
            qDebug()<<reply->readAll();
            }

            void ServiceProvider::pushImage()
            {
            manager = new QNetworkAccessManager(this);
            connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));
            connect(manager,SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
            SLOT(provideAuthenication(QNetworkReply*,QAuthenticator*)));
            manager->get(QNetworkRequest(QUrl("https://localhost/conductor/api/provider_accounts.xml")));

            }@

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              For future reference, once data in a sequential stream is read it is no longer available to read. In your original listing at line 3 your debug statement read all the available data to display. At line 4 there was no more data to read so line 5 would always display nothing.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                niteshnarayanlal
                wrote on last edited by
                #7

                But I am not getting anything , I am getting error due to replyFinished check .

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  But which error are you getting?

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    niteshnarayanlal
                    wrote on last edited by
                    #9

                    Sorry for so dealyed reply , actually I was busy :P .
                    Anyways the error which I am getting is :QNetworkReply::SslHandshakeFailedError which has error code as 6 .

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved