使用 gSOAP 通过 HTTP 和 HTTPS 调用由 WSAD 创建的 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 服务。

使用 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 服务

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

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

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

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

使用 gSOAP 通过 HTTP 和 HTTPS 调用由 WSAD 创建的 J2EE Web 服务

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

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

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

清单 1. 使用 wsdl2h 编译 WSDL 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
清单 2. 使用 soapcpp2 生成 C/C++ 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

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

表 1. gSOAP C/C++ 文件
  1. 创建 Microsoft Visual C++ 6.0 Win32 控制台应用程序。
图 3. 创建 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 复制的。
图 4. 将 gSOAP C/C++ 文件插入 VC++ 6.0 项目中

将 gSOAP C/C++ 文件插入到此 VC++ 6.0 项目中。

  1. 为 soapC.c、soapClient.c 和 stdsoap2.cpp 选择 Not using precompiled headers,因为它们并不依赖于 stdafx.h。
图 5. 选择“Not using precompiled headers”

选择“Not using precompiled headers”

  1. 链接 wsock32.lib。
图 6. 链接 wsock32.lib

链接 wsock32.lib

  1. 编写代码来使用股票报价 Web 服务。
清单 3. 利用 gSOAP 来使用股票报价 Web 服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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. 对其进行编译并运行。
图 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 服务。

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

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

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

将 OPENSSL\LIB\VC 添加到 VC++ 6 Library 目录

  1. 将 libeay32.lib 和 ssleay32.lib 添加到 Link library modules。
图 10. 将 libeay32.lib 和 ssleay32.lib 添加到 Link library modules

将 libeay32.lib 和 ssleay32.lib 添加到 Link library modules

  1. 定义 WITH_OPENSSL。
图 11. 定义 WITH_OPENSSL

定义 WITH_OPENSSL

  1. 编写代码来让 gSOAP 支持 HTTPS。
清单 4. 让 gSOAP 支持 HTTPS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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. 对其进行编译并运行。
图 12. 运行代码

运行代码

结束语

本文演示了如何使用 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 企业应用程序集成。

 
下载资源

转载于:https://www.cnblogs.com/lifan3a/articles/7478315.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值