Skip to main content

skip to main content

developerWorks  >  SOA and Web services  >

Use gSOAP to consume J2EE Web services created by WSAD through HTTP and HTTPS

developerWorks
Document options

Document options requiring JavaScript are not displayed

Sample code


Rate this page

Help us improve this content


Level: Advanced

Bo Xie (xiebo@cn.ibm.com), Software Engineer, IBM China Development Lab

24 Oct 2006

Use gSOAP as a C/C++ Web services stack to consume Java™ 2 Platform, Enterprise Edition (J2EE) Web services through HTTP and HTTPS.

Introduction

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.



Back to top


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 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
The Stock Quote Web service is ready for HTTP and HTTPS


Back to top


Use gSOAP to consume the stock quote Web service through HTTP

  1. Download gsoap_win32_2.7.7.zip (see Resources).
  2. 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 NameDescription
soapStub.hA modified and annotated header file produced from the input header file
soapH.hMain header file to be included by all client and service sources
soapC.cSerializers and deserializers for the specified data structures
soapClient.cClient stub routines for remote operations
stdsoap2.hHeader file of stdsoap2.cpp run-time library
stdsoap2.cppRun-time C++ library with XML parser and run-time support routines


  1. Create a Microsoft Visual C++ 6.0 Win32 console application.

Figure 3. Create a Microsoft Visual C++ 6.0 Win32 console application
Create a Microsoft Visual C++ 6.0 Win32 Console Application
  1. 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
Insert gSOAP C/C++ files into this VC++ 6.0 project
  1. 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
Select Not using precompiled headers
  1. Link wsock32.lib.

Figure 6. Link wsock32.lib
Link wsock32.lib
  1. 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}
};

  1. Compile and run it.

Figure 7. Run it
Run it


Back to top


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.

  1. Add OPENSSL\INCLUDE to the VC++ 6 Include directory.

Figure 8. Add OPENSSL\INCLUDE to VC++ 6 Include directory
Add OPENSSL\INCLUDE to VC++ 6 Include directory
  1. Add OPENSSL\LIB\VC to the VC++ 6 Library directory.

Figure 9. Add OPENSSL\LIB\VC to VC++ 6 Library directory
Add OPENSSL\LIB\VC to VC++ 6 Library directory
  1. Add libeay32.lib and ssleay32.lib to Link library modules.

Figure 10. Add libeay32.lib and ssleay32.lib to Link library modules
Add libeay32.lib and ssleay32.lib to Link library modules
  1. Define WITH_OPENSSL.

Figure 11. Define WITH_OPENSSL
Define WITH_OPENSSL
  1. 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}
};

  1. Compile and run it.

Figure 12. Run it
Run it


Back to top


Conclusion

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.


Back to top


Downloads

DescriptionNameSizeDownload method
WSAD stock Web service sampleWSAD_Workspace.zip1540KBHTTP
gSOAP HTTP sampleTestGSOAP.zip206KBHTTP
gSOAP HTTPS sampleTestGSOAP_SSL.zip238KBHTTP
Information about download methods


Resources

Learn

Get products and technologies
  • Download gSOAP from SourceForge.

  • Get the Windows version of OpenSSL.


Discuss


About the author

Bo Xie is a software engineer on the IBM China Development Lab based in Shanghai, China. His interests include C/C++ Web services stack and application development with WebSphere Studio Application Developer.




Rate this page


Please take a moment to complete this form to help us better serve you.



YesNoDon't know
 


 


12345
Not
useful
Extremely
useful
 


Back to top