1

I am trying to create a client application for web service. In that application i need to pass some integer number with that web service link, then web service give me some data in XML format. I able to complete ping that web service, but i don't no how to pass integer value with that web service. Please give me some sample code for request-response web service in c++. The response data is in XML and i want to store in some text file or in vector. That data i need for print using printer.

Here is my code that i develop for web service:

void Downloader::doDownload()
{
    manager = new QNetworkAccessManager(this);
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("http://api.flickr.com/services/soap/")));
    exit(0);
}
void Downloader::replyFinished (QNetworkReply *reply)
{
    if(reply->error())
    {
      qDebug() << "ERROR!!!!";
      qDebug() << reply->errorString();
    }
    else
    {
        qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
        qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
        qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
        qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();

        QFile *file = new QFile("/root/Downloads/QT Web Services/WebServiceResponseData.txt");
        if(file->open(QFile::Append))
        {
        file->write(reply->readAll());
        file->flush();
        file->close();
        }
        delete file;
    }

    reply->deleteLater();
}
  • What is the exact problem you are having with your current code? If you are just looking for an example for using `QNetworkAccessManager`, have a look at [this answer](https://stackoverflow.com/a/39521698/2666212). – Mike Nov 28 '16 at 06:04
  • Thank You Mike sir for reply. In your answer you parse the JSON, but i need XML. What i exact want is, i have to connect server using web service. on that server customer data store, i only need to pass customer id with that web service then server give me data of that particular customer in XML, i want to store that xml file to later use for printing that data on paper using printer. – sanket karanjule Nov 28 '16 at 06:33
  • Take a look at http://doc.qt.io/qt-5/xml-processing.html, for handling/processing XML with Qt. – ManuelH Nov 28 '16 at 08:38

2 Answers2

0

I might be wrong, but you may require to pass POST or GET parameters to pass your number.

GET :

GET wil lrequire you to add arguments at the end of the URL like this : http://yoururl.com/page?arg1=1&arg2=2&arg3=3

//Replace "arg" by the argument name provided by the API documentation, something like "?xml"
QString urlString = QString("http://api.flickr.com/services/soap?arg=%1").args(QString::number(yourNumber))
manager->get(QNetworkRequest(QUrl(urlString)));

POST :

POST will require you to use a QUrlQuery object to add the parameters required

QUrlQuery *postData = new QUrlQuery;
//Replace "arg" by the argument name provided by the API documentation, something like "xml"
postData->addQueryItem("arg", yourNumber);
manager->post(QNetworkRequest(QUrl("http://api.flickr.com/services/soap/")), postData->toString(QUrl::FullyEncoded).toUtf8());
Elcan
  • 814
  • 13
  • 27
  • This is not right. You can use `QUrlQuery` with GET requests, see [this answer](https://stackoverflow.com/a/15620789/2666212). In fact, using `QUrlQuery` is far better than using `QString::arg()` to build the URL yourself since the former takes care of escaping characters automatically when needed. – Mike Nov 28 '16 at 11:57
  • Thank you sir for reply. Basically what I want to do is, i have a web service URL, i want to make a client application for accessing the web service methods. How i use methods of that web service in my application? i.e. developed in QT C++ – sanket karanjule Dec 08 '16 at 09:10
0

You need SOAP?

Use KDSoap for that: https://github.com/KDAB/KDSoap

There are examples with and without code generation from *.wsdl: https://github.com/KDAB/KDSoap/tree/master/examples

Velkan
  • 7,067
  • 6
  • 43
  • 87