Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2659397
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2007-10-13 09:23:33

级别: 高级

Bo Xie (), 软件工程师, IBM China Development Lab

2007 年 1 月 25 日

使用 gSOAP 作为 C/C++ Web 服务栈,以通过 HTTP 和 HTTPS 调用 Java™ 2 Platform Enterprise Edition (J2EE) Web 服务。

很多公司都提供基于 Java™ 的 Web 服务栈,包括 Apache 的 Axis、IBM 的 WebSphere® Studio Application Developer (WSAD) 和 BEA 的 WebLogic Web 服务。Microsoft® .NET 技术提供一些用于 Web 服务的工具,如 Web Services Enhancements (WSE) 3.0 等。但如果要在所有平台(特别是嵌入式系统)允许(遗留) C/C++ 代码使用 Web 服务,且希望内存占用较小,C/C++ Web 服务栈就是最佳选择。最理想的选择是 gSOAP,此 Web 服务栈针对 C/C++ 进行了优化(请参见参考资料)。它提供了 SOAP/XML-to-C/C++ 语言绑定,用于简化采用 C/C++ 开发 SOAP/XML Web 服务和客户机应用程序的工作。

接下来让我们详细了解一下如何使用 gSOAP 通过 HTTP 和 HTTPS 调用由 WSAD 创建的 J2EE Web 服务。





回页首


IBM 提供了一篇非常优秀的教程“”。通过阅读该教程,可了解如何构建我们将在本文中作为示例使用的股票报价服务。

请遵循该教程中列出的步骤创建股票报价服务。完成相关工作获得了该服务后,请右键单击 WebProject 并选择 Run on Server



使用 WSAD 5.1.2 部署股票报价 Web 服务

股票报价 Web 服务现在已经就绪,可供使用了。端口 9080 用于 HTTP,而端口 9443 用于 HTTPS。



股票报价 Web 服务已就绪,可通过 HTTP 和 HTTPS 进行使用




回页首


  1. 下载 gsoap_win32_2.7.7.zip(请参见参考资料)。
  2. 从 WSDL 文件创建 C/C++ 文件。

注意:可以在 WSAD_WorkSpace.zip(请参见下载)中找到该股票报价 Web 服务的 WSDL 文件,名为 StockQuoteService.wsdl。

接下来,您将了解如何使用 gSOAP 的 wsdl2hsoapcpp2 工具来从 WSDL 文件创建 C/C++ 文件。



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



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: 
Service
Using ns1 schema namespace: 
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

表 1 显示了各个 gSOAP C/C++ 文件的意义。



文件名称 描述
soapStub.h 从输入 Header 文件生成的经过修改且带标注的 Header 文件
soapH.h 主 Header 文件,所有客户机和服务源代码都要将其包括在内
soapC.c 指定数据结构的序列化器和反序列化器
soapClient.c 远程操作的客户机存根例程
stdsoap2.h stdsoap2.cpp 运行时库的 Header 文件
stdsoap2.cpp 运行时 C++ 库,带 XML 解析器和运行时支持例程


  1. 创建 Microsoft Visual C++ 6.0 Win32 控制台应用程序。


创建 Microsoft Visual C++ 6.0 Win32 控制台应用程序
  1. 将 gSOAP C/C++ 文件插入到此 VC++ 6.0 项目中。注意:stdsoap2.h 和 stdsoap2.cpp 是从 gsoap_win32_2.7.7.zip 复制的。


将 gSOAP C/C++ 文件插入到此 VC++ 6.0 项目中。
  1. 为 soapC.c、soapClient.c 和 stdsoap2.cpp 选择 Not using precompiled headers,因为它们并不依赖于 stdafx.h。


选择“Not using precompiled headers”
  1. 链接 wsock32.lib。


链接 wsock32.lib
  1. 编写代码来使用股票报价 Web 服务。


#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 \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", ""},    /* MUST be first */
  {"SOAP-ENC", ""},    /* MUST be second */
  {"xsi", ""},         /* MUST be third */
  {"xsd", ""},
  {"ns1", ""},       /* Method namespace URI */
  {NULL, NULL}
};

  1. 对其进行编译并运行。


运行代码




回页首


在生产环境中运行的应用程序通常需要进行身份验证和授权,以跟踪用户和记录其特有的数据。HTTPS 上的安全套接字层(Secure Sockets Layer,SSL)加密等传输级别的安全机制在协议级别工作,会对各个 Web 服务调用的起始点和结束点之间的所有数据包进行加密。WSAD 提供了内置 HTTPS 函数,将同时侦听 9080 端口(用于 HTTP)和 9443 端口(用于 HTTPS)。要支持 HTTPS,只需要对 gSOAP 进行配置。在您的平台上安装 OpenSSL 库(请参见参考资料),以允许安全 SOAP 客户机使用 HTTPS/SSL。安装完成后,使用 -DWITH_OPENSSL 选项编译应用程序的所有源文件。

现在让我们一步步进行此工作。将 TestGSOAP VC++ 项目重命名为 TestGSOAP_SSL,我们将通过 HTTPS 为 TestGSOAP_SSL VC++ 项目使用股票报价 Web 服务。

  1. 将 OPENSSL\INCLUDE 添加到 VC++ 6 Include 目录。


将 OPENSSL\INCLUDE 添加到 VC++ 6 Include 目录
  1. 将 OPENSSL\LIB\VC 添加到 VC++ 6 Library 目录。


将 OPENSSL\LIB\VC 添加到 VC++ 6 Library 目录
  1. 将 libeay32.lib 和 ssleay32.lib 添加到 Link library modules。


将 libeay32.lib 和 ssleay32.lib 添加到 Link library modules
  1. 定义 WITH_OPENSSL。


定义 WITH_OPENSSL
  1. 编写代码来让 gSOAP 支持 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 \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, 
      "/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", ""},    /* MUST be first */
  {"SOAP-ENC", ""},    /* MUST be second */
  {"xsi", ""},         /* MUST be third */
  {"xsd", ""},
  {"ns1", ""},       /* Method namespace URI */
  {NULL, NULL}
};

  1. 对其进行编译并运行。


运行代码




回页首


本文演示了如何使用 gSOAP 作为 C/C++ Web 服务栈来通过 HTTP 和 HTTPS 使用 J2EE Web 服务。您已经了解了如何进行以下工作:

  • 下载、安装和配置 gSOAP 与 OpenSSL。
  • 生成 C/C++ Windows 控制台应用程序来通过 HTTP 使用 J2EE Web 服务。
  • 生成 C/C++ Windows 控制台应用程序来通过 HTTPS 使用 J2EE Web 服务。
这些知识可以帮助您开始着手使用 WSAD 和 gSOAP 将 C/C++ 应用程序与 J2EE 企业应用程序集成。




回页首


描述 名字 大小 下载方法
WSAD stock Web service sample WSAD_Workspace.zip 1540KB
gSOAP HTTP sample TestGSOAP.zip 206KB
gSOAP HTTPS sample TestGSOAP_SSL.zip 238KB
关于下载方法的信息


学习

获得产品和技术
  • 从 SourceForge 下载 。

  • 获得 的 Windows 版本。


讨论


Bo Xie 是位于中国上海的 IBM 中国开发中心的一位软件工程师。他感兴趣的领域包括 C/C++ Web 服务栈和使用 WebSphere Studio Application Developer 进行应用程序开发。

阅读(1152) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~