Writing a simple SOAP client / server in C++ using gsoap
This is how I wrote a simple SOAP server in C++ using gsoap on my Debian Lenny server.
Preparing things
gsoap can be easily installed with the usual apt-get install gsoap
My service will be defined based on O’Reilly’s sample HelloService.wsdl which is available in Web Services Essential
Generating the skeleton files
The header file can be generated based on the .wsdl with the help of wsdl2h:
wsdl2h -o header.h HelloService.wsdl
Then it’s possible to generate the skeleton .ccp files with gsoap:
soapcpp2 -I/usr/include/gsoap header.h
You’ll note as well many other files generated - including .xml with sample queries, .h files…
Generating a command-line server
For a start we’re going to generate a command-line SOAP server.
All you need is to create a file named HelloServiceCLIServer.cpp with the following content:
// Contents of file "HelloService.cpp":
#include "soapH.h"
#include "Hello_USCOREBinding.nsmap"
main ()
{
soap_serve (soap_new ());
}
int ns1__sayHello (struct soap *soap, std::string firstName,
std::string & greeting)
{
greeting = "Hello my dear " + firstName + ".";
return SOAP_OK;
}
Then compile with:
g++ -lgsoap++ -lgsoapck++ HelloServiceCLIServer.cpp /usr/include/gsoap/stdsoap2.cpp soapC.cpp soapServer.cpp -o HelloServiceCLIServer
All done - you can test you server by using the sample .xml request file provided (it has an empty name, but that’s good enough for a crude test):
cat Hello_USCOREBinding.sayHello.req.xml | ./HelloServiceCLIServer
You will see a SOAP answer with the expected greeting (Hello my dear .)