Hi,
I am new to this forum. So a big hello to all.
I am trying to talk to a server using its Xml api. I am using QXmlStreamReader for my xml operations and QNetworkAccessManager and family of classes for network related stuff.
The problem, I am facing is that the xml request fails every time. The response I get from server says that my xml is incorrectly formatted.
Following is the test class :
{
Q_OBJECT
public :
virtual ~Tool();
void listClients();
public slots :
void readXml( QNetworkReply* );
void slotError(QNetworkReply * reply, QAuthenticator * authenticator);
private :
QNetworkAccessManager manager;
};
class Tool : public QObject
{
Q_OBJECT
public :
Tool( QObject *parent = 0 );
virtual ~Tool();
void listClients();
public slots :
void readXml( QNetworkReply* );
void slotError(QNetworkReply * reply, QAuthenticator * authenticator);
private :
QNetworkAccessManager manager;
};
To copy to clipboard, switch view to plain text mode
This method is simply reading the response and parsing it using QXmlStreamReader. Please note the comment near the if condition that checks for tag called "error".
void Tool::readXml( QNetworkReply* reply )
{
qDebug() << "readXml()" ;
QXmlStreamReader xmlReply( reply );
while( !xmlReply.atEnd() ) {
qDebug() << "Reading next element.";
xmlReply.readNext();
if (xmlReply.isEndElement()) {
qDebug() << "Found END element for " << xmlReply.name() ;
break;
}
if (xmlReply.isStartElement()) {
qDebug() << "Found START element for " << xmlReply.name();
if (xmlReply.name() == "response")
qDebug() << "Response Status : " << xmlReply.attributes().value("status");
else if ( xmlReply.name() == "name" )
qDebug() << "Name : " << xmlReply.readElementText();
else if ( xmlReply.name() == "error" )
qDebug() << "Error : " << xmlReply.readElementText();
//This is the error condition that is hit all the time. And says that the xml is not correctly formatted.
}//isStartElement
}
if (xmlReply.hasError()) {
qDebug() << "Xml Reply has errors." << xmlReply.errorString();
qDebug() << xmlReply.error();
}
}
void Tool::readXml( QNetworkReply* reply )
{
qDebug() << "readXml()" ;
QXmlStreamReader xmlReply( reply );
while( !xmlReply.atEnd() ) {
qDebug() << "Reading next element.";
xmlReply.readNext();
if (xmlReply.isEndElement()) {
qDebug() << "Found END element for " << xmlReply.name() ;
break;
}
if (xmlReply.isStartElement()) {
qDebug() << "Found START element for " << xmlReply.name();
if (xmlReply.name() == "response")
qDebug() << "Response Status : " << xmlReply.attributes().value("status");
else if ( xmlReply.name() == "name" )
qDebug() << "Name : " << xmlReply.readElementText();
else if ( xmlReply.name() == "error" )
qDebug() << "Error : " << xmlReply.readElementText();
//This is the error condition that is hit all the time. And says that the xml is not correctly formatted.
}//isStartElement
}
if (xmlReply.hasError()) {
qDebug() << "Xml Reply has errors." << xmlReply.errorString();
qDebug() << xmlReply.error();
}
}
To copy to clipboard, switch view to plain text mode
This is where the xml is prepared and the request to the server is made.
void Tool::listClients()
{
qDebug() << "listClients" ;
QNetworkRequest request;
request.setUrl(serviceUrl);
request.setRawHeader("User-Agent", "Tool 0.0.1");
std::stringstream strstream;
strstream << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
<< "<request method=\"client.list\">"
<< "</request>";
std::cout << "Posted Xml : " << strstream.str();
manager.post(request, data);
}
void Tool::listClients()
{
qDebug() << "listClients" ;
QNetworkRequest request;
request.setUrl(serviceUrl);
request.setRawHeader("User-Agent", "Tool 0.0.1");
std::stringstream strstream;
strstream << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
<< "<request method=\"client.list\">"
<< "</request>";
std::cout << "Posted Xml : " << strstream.str();
QByteArray data(strstream.str().c_str());
manager.post(request, data);
}
To copy to clipboard, switch view to plain text mode
I have tried to use QByteArray directly to prepare the xml and have got the same results.
So, what I want to know is, what am I doing wrong? Also, I am wondering if the
QNetworkAccessManager::post()
QNetworkAccessManager::post()
To copy to clipboard, switch view to plain text mode
method is sending solely XML in the request body or is it sending in something else as well? If it is sending more than the prepared XML as request, then what is the right way to send only the XML.
Thanks.
Bookmarks