Monday, December 14, 2009

Creating a web service client using gsoap.

In this post I'll continue my posts related to creating web services (Part 1, Part 2), as have been shown it's easy to setup and create the server part of the service using Java, Axis2 and Tomcat. However you do not always have access to a very hungry and memory intensive Java stack so this post will cover how you can create the client in native C code. For this I'll be using gSOAP. If you have the web service up and running as describe previously you should be able to access it's WSDL document at http://localhost:8080/axis2/services/ExampleService?wsdl. The WSDL document describes the service in detail and can be used to generate the needed code to access the service. First you start off by installing the package:
  • apt-get install gsoap
Now you can use the wsdl2h tool to generate C bindings (it can also be used to generate C++ bindings, just drop the -c argument)
  • wsdl2h -c -o mywebservice.h http://localhost:8080/axis2/services/ExampleService?wsdl
  • soapcpp2 -C -c mywebservice.h -I/usr/include/gsoap
The following very simple code segment shows how to call the remote service, both the getSomeValue() and setSomeValue() function. It can be compiled by doing:
  • gcc -I/usr/include/gsoap myclient.c -o myclient.o soapC.c soapClient.c /usr/include/gsoap/stdsoap2.c
myclient.c
#include "soapH.h"
#include "stdio.h"
#include "MyWebService.nsmap"

int main(int argc, char *argv[])
{
struct soap *soap = soap_new();
struct _ns1__getSomeValueResponse response;
struct _ns1__setSomeValue value;

*value.args0 = 1.0f;

if (soap_send___ns2__setSomeValue(soap, NULL, NULL, &value) == SOAP_OK)
printf("okay");
else
soap_print_fault(soap, stderr); // display the SOAP fault on the stderr stream

if (soap_call___ns2__getSomeValue(soap, NULL, NULL, &response) == SOAP_OK)
printf("value: %f\n", *response.return_);
else // an error occurred
soap_print_fault(soap, stderr); // display the SOAP fault on the stderr stream
}

No comments:

Post a Comment