QNetworkAccessManager and xml reply
-
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]
-
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.
-
thanks for replying , but both of them are not showing anything . I am getting a blank reply .
-
Check the error() method in your network reply before you try to read anything. See if there is an error being set.
-
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")));}@
-
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.
-
But I am not getting anything , I am getting error due to replyFinished check .
-
But which error are you getting?
-
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 .