很多公司都提供基于 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 服务。
|
使用 WSAD 5.1.2 创建和部署股票报价 Web 服务
IBM 提供了一篇非常优秀的教程“Creating and deploying the Stock Quote Web service from a Java bean using the WebSphere V5 run-time environment”。通过阅读该教程,可了解如何构建我们将在本文中作为示例使用的股票报价服务。
请遵循该教程中列出的步骤创建股票报价服务。完成相关工作获得了该服务后,请右键单击 WebProject 并选择 Run on Server。
图 1. 使用 WSAD 5.1.2 部署股票报价 Web 服务
股票报价 Web 服务现在已经就绪,可供使用了。端口 9080 用于 HTTP,而端口 9443 用于 HTTPS。
图 2. 股票报价 Web 服务已就绪,可通过 HTTP 和 HTTPS 进行使用
|
使用 gSOAP 通过 HTTP 和 HTTPS 调用由 WSAD 创建的 J2EE Web 服务
- 下载 gsoap_win32_2.7.7.zip(请参见参考资料)。
- 从 WSDL 文件创建 C/C++ 文件。
注意:可以在 WSAD_WorkSpace.zip(请参见下载)中找到该股票报价 Web 服务的 WSDL 文件,名为 StockQuoteService.wsdl。
接下来,您将了解如何使用 gSOAP 的 wsdl2h
和 soapcpp2
工具来从 WSDL 文件创建 C/C++ 文件。
清单 1. 使用 wsdl2h 编译 WSDL 文件
C:/>wsdl2h -c StockQuoteService.wsdl |
清单 2. 使用 soapcpp2 生成 C/C++ 文件
C:/>soapcpp2 -c -C StockQuoteService.h |
表 1 显示了各个 gSOAP C/C++ 文件的意义。
表 1. gSOAP C/C++ 文件
文件名称 | 描述 |
---|---|
soapStub.h | 从输入 Header 文件生成的经过修改且带标注的 Header 文件 |
soapH.h | 主 Header 文件,所有客户机和服务源代码都要将其包括在内 |
soapC.c | 指定数据结构的序列化器和反序列化器 |
soapClient.c | 远程操作的客户机存根例程 |
stdsoap2.h | stdsoap2.cpp 运行时库的 Header 文件 |
stdsoap2.cpp | 运行时 C++ 库,带 XML 解析器和运行时支持例程 |
- 创建 Microsoft Visual C++ 6.0 Win32 控制台应用程序。
图 3. 创建 Microsoft Visual C++ 6.0 Win32 控制台应用程序
- 将 gSOAP C/C++ 文件插入到此 VC++ 6.0 项目中。注意:stdsoap2.h 和 stdsoap2.cpp 是从 gsoap_win32_2.7.7.zip 复制的。
图 4. 将 gSOAP C/C++ 文件插入 VC++ 6.0 项目中
- 为 soapC.c、soapClient.c 和 stdsoap2.cpp 选择 Not using precompiled headers,因为它们并不依赖于 stdafx.h。
图 5. 选择“Not using precompiled headers”
- 链接 wsock32.lib。
图 6. 链接 wsock32.lib
- 编写代码来使用股票报价 Web 服务。
清单 3. 利用 gSOAP 来使用股票报价 Web 服务
#include "stdafx.h" |
- 对其进行编译并运行。
图 7. 运行代码
|
使用 gSOAP 通过 HTTPS 调用股票报价 Web 服务
在生产环境中运行的应用程序通常需要进行身份验证和授权,以跟踪用户和记录其特有的数据。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 服务。
- 将 OPENSSL/INCLUDE 添加到 VC++ 6 Include 目录。
图 8. 将 OPENSSL/INCLUDE 添加到 VC++ 6 Include 目录
- 将 OPENSSL/LIB/VC 添加到 VC++ 6 Library 目录。
图 9. 将 OPENSSL/LIB/VC 添加到 VC++ 6 Library 目录
- 将 libeay32.lib 和 ssleay32.lib 添加到 Link library modules。
图 10. 将 libeay32.lib 和 ssleay32.lib 添加到 Link library modules
- 定义 WITH_OPENSSL。
图 11. 定义 WITH_OPENSSL
- 编写代码来让 gSOAP 支持 HTTPS。
清单 4. 让 gSOAP 支持 HTTPS
#include "stdafx.h" |
- 对其进行编译并运行。
图 12. 运行代码
|
本文演示了如何使用 gSOAP 作为 C/C++ Web 服务栈来通过 HTTP 和 HTTPS 使用 J2EE Web 服务。您已经了解了如何进行以下工作:
- 下载、安装和配置 gSOAP 与 OpenSSL。
- 生成 C/C++ Windows 控制台应用程序来通过 HTTP 使用 J2EE Web 服务。
- 生成 C/C++ Windows 控制台应用程序来通过 HTTPS 使用 J2EE Web 服务。
|
描述 | 名字 | 大小 | 下载方法 |
---|---|---|---|
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 |
关于下载方法的信息 | Get Adobe® Reader® |
学习
- 您可以参阅本文在 developerWorks 全球站点上的 英文原文 。
- 教程“Creating and deploying the Stock Quote Web service from a Java bean using the WebSphere V5 run-time environment”将会帮助您创建股票报价 Web 服务。
- 了解有关 gSOAP 的更多信息,获得 gSOAP User Guide。
- 了解关于 developerWorks 技术事件与网络广播的最新消息。
- IBM SOA 网站提供了 SOA 的概述,并说明 IBM 可以如何帮助您实现此目标。
- 有关 SOA 的更多信息,请访问 developerWorks SOA and Web services 专区。
获得产品和技术
讨论
- 参与 developerWorks 博客,从而加入到 developerWorks 社区中来。
- 与 SOA and Web services 讨论论坛中的架构师和开发人员社区协作。
Bo Xie 是位于中国上海的 IBM 中国开发中心的一位软件工程师。他感兴趣的领域包括 C/C++ Web 服务栈和使用 WebSphere Studio Application Developer 进行应用程序开发。 |
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1676795