Many companies offer Java™-based Web services stacks, including Axis from Apache, WebSphere® Studio Application Developer (WSAD) from IBM, and WebLogic Web services from BEA. Microsoft® .NET technology provides some tools for Web services, such as Web Services Enhancements (WSE) 3.0. But if you want to enable (legacy) C/C++ code to consume Web services on all platforms (especially for embedded systems) with a small memory footprint, a C/C++ Web services stack is your best option. And the ideal choice is gSOAP, a Web services stack that's optimized for C/C++ (see Resources). It provides a SOAP/XML-to-C/C++ language binding to ease the development of SOAP/XML Web services and client applications in C/C++.
Let's take a step-by-step look at how to use gSOAP to consume J2EE Web services through HTTP and HTTPS.
Create and deploy a stock quote Web service using WSAD 5.1.2
IBM offers an excellent tutorial, "Creating and deploying the Stock Quote Web service from a Java bean using the WebSphere V5 run-time environment". Looking through this tutorial will help you create a stock quote service, which we will use as an example throughout this article.
Please go through the steps that are outlined in that tutorial and create the stock quote service. Once you have done that, and you have your service, right-click WebProject and select Run on Server.
Figure 1. Deploy the stock quote Web service using WSAD 5.1.2

A stock quote Web service is now ready for you. Port 9080 is ready for HTTP and port 9443 is ready for HTTPS.
Figure 2. The stock quote Web service is ready for HTTP and HTTPS

Use gSOAP to consume the stock quote Web service through HTTP
- Download gsoap_win32_2.7.7.zip (see Resources).
- Create C/C++ files from the WSDL file.
Note: You can find the stock quote Web service WSDL file, called StockQuoteService.wsdl, in WSAD_WorkSpace.zip (see Downloads).
Next you'll see how to use gSOAP's wsdl2h and soapcpp2 tools to create C/C++ files from the WSDL file.
Listing 1. Use wsdl2h to compile the WSDL file
C:\>wsdl2h -c StockQuoteService.wsdl ** The gSOAP WSDL parser for C and C++ 1.2.7 ** Copyright (C) 2000-2006 Robert van Engelen, Genivia Inc. ** All Rights Reserved. This product is provided "as is", without any warranty. ** The gSOAP WSDL parser is released under one of the following two licenses: ** GPL or the commercial license by Genivia Inc. Use option -l for more info. Saving StockQuoteService.h Cannot open file 'typemap.dat' Problem reading type map file typemap.dat. Using internal type definitions for C instead. Reading file 'StockQuoteService.wsdl' To complete the process, compile with:soapcpp2 StockQuoteService.h |
Listing 2. Use soapcpp2 to generate C/C++ files
C:\>soapcpp2 -c -C StockQuoteService.h ** The gSOAP Stub and Skeleton Compiler for C and C++ 2.7.7 ** Copyright (C) 2000-2006, Robert van Engelen, Genivia Inc. ** All Rights Reserved. This product is provided "as is", without any warranty. ** The gSOAP compiler is released under one of the following three licenses: ** GPL, the gSOAP public license, or the commercial license by Genivia Inc. Saving soapStub.h Saving soapH.h Saving soapC.c Saving soapClient.c Saving soapClientLib.c Using ns1 service name: StockQuoteServiceSoapBinding Using ns1 service style: document Using ns1 service encoding: literal Using ns1 service location: http://localhost:9080/WebProject/services/StockQuote Service Using ns1 schema namespace: http://stockquote Saving StockQuoteServiceSoapBinding.getQuote.req.xml sample SOAP/XML request Saving StockQuoteServiceSoapBinding.getQuote.res.xml sample SOAP/XML response Saving StockQuoteServiceSoapBinding.nsmap namespace mapping table Compilation successful |
Table 1 shows the meaning of the gSOAP C/C++ files.
Table 1. gSOAP C/C++ files
| File Name | Description |
|---|---|
| soapStub.h | A modified and annotated header file produced from the input header file |
| soapH.h | Main header file to be included by all client and service sources |
| soapC.c | Serializers and deserializers for the specified data structures |
| soapClient.c | Client stub routines for remote operations |
| stdsoap2.h | Header file of stdsoap2.cpp run-time library |
| stdsoap2.cpp | Run-time C++ library with XML parser and run-time support routines |
- Create a Microsoft Visual C++ 6.0 Win32 console application.
Figure 3. Create a Microsoft Visual C++ 6.0 Win32 console application

- Insert the gSOAP C/C++ files into this VC++ 6.0 project. Note: stdsoap2.h and stdsoap2.cpp are copied from gsoap_win32_2.7.7.zip.
Figure 4. Insert gSOAP C/C++ files into the VC++ 6.0 project

- Select Not using precompiled headers for soapC.c, soapClient.c, and stdsoap2.cpp because they don't depend on stdafx.h.
Figure 5. Select Not using precompiled headers

- Link wsock32.lib.
Figure 6. Link wsock32.lib

- Write code to consume the Stock Quote Web service.
Listing 3. Use gSOAP to consume the stock quote Web service
#include "stdafx.h"
#include "soapH.h" /* include generated proxy and SOAP support */
int main(int argc, char* argv[])
{
_ns1__getQuote getQuote;
_ns1__getQuoteResponse getQuoteResponse;
struct soap soap;
if (argc > 1)
getQuote.symbol = argv[1];
else
{
fprintf(stderr, "Usage: quote <ticker>\n");
return -1;
}
soap_init(&soap);
if (soap_call___ns1__getQuote(&soap, NULL, NULL, &getQuote, &getQuoteResponse) == 0)
printf(
"\nCompany - %s Quote - %f\n", getQuote.symbol, getQuoteResponse.getQuoteReturn);
else
soap_print_fault(&soap, stderr);
return 0;
}
/* The namespace mapping table is required and associates
namespace prefixes with namespace names: */
struct Namespace namespaces[] =
{
{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"}, /* MUST be first */
{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/"}, /* MUST be second */
{"xsi", "http://www.w3.org/1999/XMLSchema-instance"}, /* MUST be third */
{"xsd", "http://www.w3.org/1999/XMLSchema"},
{"ns1", "http://stockquote"}, /* Method namespace URI */
{NULL, NULL}
};
|
- Compile and run it.
Figure 7. Run it

Use gSOAP to call the Stock Quote Web service through HTTPS
Applications running in production environments usually require authentication and authorization to track users and to store their unique data. Transport-level security, such as Secure Sockets Layer (SSL) encryption over HTTPS, works at the protocol level and encrypts all the packets between the start point and end point of an individual Web service call. WSAD has a built-in HTTPS function that listens on port 9080 for HTTP and port 9443 for HTTPS simultaneously. Only gSOAP needs to be configured to support HTTPS. Install the OpenSSL library (see Resources) on your platform to enable secure SOAP clients to use HTTPS/SSL. After installation, compile all the sources of your application with option -DWITH_OPENSSL.
Now, let's do it step-by-step. Rename the TestGSOAP VC++ project to TestGSOAP_SSL, and we'll consume the stock quote Web service through HTTPS for the TestGSOAP_SSL VC++ project.
- Add OPENSSL\INCLUDE to the VC++ 6 Include directory.
Figure 8. Add OPENSSL\INCLUDE to VC++ 6 Include directory

- Add OPENSSL\LIB\VC to the VC++ 6 Library directory.
Figure 9. Add OPENSSL\LIB\VC to VC++ 6 Library directory

- Add libeay32.lib and ssleay32.lib to Link library modules.
Figure 10. Add libeay32.lib and ssleay32.lib to Link library modules

- Define WITH_OPENSSL.
Figure 11. Define WITH_OPENSSL

- Write code to let gSOAP support HTTPS.
Listing 4. Let gSOAP support HTTPS
#include "stdafx.h"
#include "soapH.h" /* include generated proxy and SOAP support */
int main(int argc, char* argv[])
{
_ns1__getQuote getQuote;
_ns1__getQuoteResponse getQuoteResponse;
struct soap soap;
if (argc > 1)
getQuote.symbol = argv[1];
else
{
fprintf(stderr, "Usage: quote <ticker>\n");
return -1;
}
soap_init(&soap);
if (soap_ssl_client_context(&soap,
SOAP_SSL_NO_AUTHENTICATION, /* use SOAP_SSL_DEFAULT in production code */
NULL, /* keyfile: required only when client must authenticate to
server (see SSL docs on how to obtain this file) */
NULL, /* password to read the keyfile */
NULL, /* optional cacert file to store trusted certificates */
NULL, /* optional capath to directory with trusted certificates */
NULL /* if randfile!=NULL: use a file with random data to seed randomness */
))
{
soap_print_fault(&soap, stderr);
exit(1);
}
if (soap_call___ns1__getQuote(&soap,
"https://localhost:9443/WebProject/services/StockQuoteService",
NULL, &getQuote, &getQuoteResponse) == 0)
printf(
"\nCompany - %s Quote - %f\n", getQuote.symbol, getQuoteResponse.getQuoteReturn);
else
soap_print_fault(&soap, stderr);
return 0;
}
/* The namespace mapping table is required and associates
namespace prefixes with namespace names: */
struct Namespace namespaces[] =
{
{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"}, /* MUST be first */
{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/"}, /* MUST be second */
{"xsi", "http://www.w3.org/1999/XMLSchema-instance"}, /* MUST be third */
{"xsd", "http://www.w3.org/1999/XMLSchema"},
{"ns1", "http://stockquote"}, /* Method namespace URI */
{NULL, NULL}
};
|
- Compile and run it.
Figure 12. Run it

This article demonstrated how to use gSOAP as a C/C++ Web services stack to consume J2EE Web services through both HTTP and HTTPS. You've learned how to do the following:
- Download, install, and configure gSOAP and OpenSSL.
- Generate a C/C++ Windows console application to consume J2EE Web services through HTTP.
- Generate a C/C++ Windows console application to consume J2EE Web services through HTTPS.
This knowledge can help you get started integrating C/C++ applications with J2EE enterprise applications using WSAD and gSOAP.
| Description | Name | Size | Download method |
|---|---|---|---|
| WSAD stock Web service sample | WSAD_Workspace.zip | 1540KB | HTTP |
| gSOAP HTTP sample | TestGSOAP.zip | 206KB | HTTP |
| gSOAP HTTPS sample | TestGSOAP_SSL.zip | 238KB | HTTP |
Information about download methods
Learn
- The tutorial "Creating and deploying the Stock Quote Web service from a Java bean using the WebSphere V5 run-time environment" will help you create a stock quote Web service.
- Learn more about gSOAP and get the gSOAP User Guide.
-
Stay current with developerWorks technical events and webcasts.
- The IBM SOA Web site offers an overview of SOA and how IBM can help you to get there.
-
Visit the SOA and Web services zone on developerWorks to learn more about SOA.
Get products and technologies
Discuss
-
Participate in developerWorks blogs and get involved in the developerWorks community.
-
Collaborate with a community of architects and developers in the SOA and Web services discussion forums.




