Qt Effort

Sep

5

How to do a SOAP webservice request using QtSOAP

By arthurdesribeiro

To do a SOAP webservice request using QtSOAP, is simple, the two main objects that you’re going to need from QtSOAP area a QtSoapHttpTransport, that in this example it’s going to be created in the constructor and a QtSoapMessage, that you can see it use later.
In this example the SOAP client call a method called “sum” that sum two numbers that are passed as parameters.

First of all let’s implement the client.h file:

#include <QDebug>
#include <qtsoap.h>

class Client : public QObject
{
    Q_OBJECT

    public:

        QtSoapHttpTransport http;
        int number1;
        int number2;

        Client(int number1, int number2, QObject *parent = 0);
        void soma();

    private slots:
        void getResponse();
};

#endif

As we could see the numbers that are going to be in the method parameters are passed in constructor of Client class.

Now implement the client.cpp

#include "client.h"

Client::Client(int number1, int number2, QObject *parent) : QObject(parent)
{
    this->number1 = number1;
    this->number2 = number2;
    http.setHost(host, port);
    // Or use just http.setHost(host)
    connect(&http, SIGNAL(responseReady()), this, SLOT(getResponse()));

}

void Client::sum()
{
    QtSoapMessage request;
    request.setMethod("sum");
    request.addMethodArgument("number1", "", number1);
    request.addMethodArgument("number2", "", number2);
    qDebug("sum of %i and %i...", number1, number2);
    http.submitRequest(request, "/soma");
}

void Client::getResponse()
{
    const QtSoapMessage &message = http.getResponse();
    if (message.isFault()) {
        qDebug("Error: %s", qPrintable(message.faultString().toString()));
    }
    qDebug()<< message.returnValue().toString();
}

In the constructor of Client class the two numbers are passed as parameter and the QtSoapHttpTransport host is set.

The sum method is the most important one, first of all you have to create the request message, that contains the method that will be called and the parameters of it. After the creation of message just submit the request to QtSoapHttpTransport host in the right path. The getResponse slot is very important, when the QtSoapHttpTransport receives the response a signal is emitted and enters in this slot, where the response message is going to be shown.

After implementing client.h and client.cpp you just have to implement the main.cpp that is gonna use this functions and it’s done.

One Response so far

Great post, thanx for info.

Leave a comment