Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

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

Bo Xie (xiebo@cn.ibm.com), Software Engineer, IBM China Development Lab
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.

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

Date:  24 Oct 2006
Level:  Advanced
Also available in:   Chinese

Activity:  11888 views
Comments:  

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.


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

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

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

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.



Downloads

DescriptionNameSizeDownload method
WSAD stock Web service sampleWSAD_Workspace.zip1540KB HTTP
gSOAP HTTP sampleTestGSOAP.zip206KB HTTP
gSOAP HTTPS sampleTestGSOAP_SSL.zip238KB HTTP

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and Web services
ArticleID=170276
ArticleTitle=Use gSOAP to consume J2EE Web services created by WSAD through HTTP and HTTPS
publish-date=10242006
author1-email=xiebo@cn.ibm.com
author1-email-cc=flanders@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers