gSOAP在VC2008中的使用

 
      gSOAP工具包是一个提供SOAP/XML Web services和通用(non-SOAP) C/C++ XML数据绑定的开源C/C++软件开发工具包。

      从 http://sourceforge.net/projects/gsoap2/files/下载最新的gSOAP包,当前最新版本为2.8.8。解压之后,在目录"\gsoap-2.8\gsoap\bin\win32"下有两个文件,分别为:
  • wsdl2h.exe        该工具转换WSDL和XSD文件到带有注释的C/C++定义文件。
  • soapcpp2.exe    该工具从带有注释的C/C/++定义文件生成RPC代码和XML序列化。
命令行介绍
使用:wsdl2h [-a] [-c] [-d] [-e] [-f] [-g] [-h] [-I path] [-i] [-j] [-k] [-l] [-m] [-n name] [-N name] [-p|-P] [-q name] [-r proxyhost[:port[:uid:pwd]]] [-s] [-t typemapfile] [-u] [-v] [-w] [-W] [-x] [-y] [-z#] [-_] [-o outfile.h] infile.wsdl infile.xsd http://www... ...
​wsdl2h选项
OptionDescription
-agenerate indexed struct names for local elements with anonymous types
为匿名类型的本地元素生成带索引结构名称
-c
generate C source code
生成C源代码 
-d
use DOM to populate xs:any and xsd:anyType elements
使用DOM填充 xs:any 和 xsd:anyType 元素
-edon't qualify enum names
This option is for backward compatibility with gSOAP 2.4.1 and earlier.
The option does not produce code that conforms to WS-I Basic Profile 1.0a.
不修饰枚举名称
-fgenerate flat C++ class hierarchy for schema extensions
-ggenerate global top-level element declarations
生成全局顶层元素声明
-hprint help information
打印帮助信息
-idon't import
不导入
-I pathuse path to locate source files for #import
让#import使用路径path查找源文件
-jdon't generate SOAP_ENV__Header and SOAP_ENV__Detail definitions
-kdon't generate SOAP_ENV__Header mustUnderstand qualifiers
-linclude license information in output
输出时包括许可证信息
-muse xsd.h module to import primitive types
使用xsd.h模块导入私钥
-n nameuse name as the base namespace prefix name instead of ns
使用名称name代替ns来作为基本的命名空间前缀名
-N nameuse name as the base namespace prefix name for service namespaces
使用名称name代替服务命名空间来作为基本的命名空间前缀名
-o fileoutput to file
输出到file文件
-pcreate polymorphic types with C++ inheritance hierarchy from base xsd__anyType
从基本的xsd__anyType创建带C++中的继承层次结构的多态类型
-Pdon't create polymorphic types with C++ inheritance hierarchy from xsd__anyType
This is automatically performed when WSDL contains polymorphic definitions
-q nameuse name for the C++ namespace of all declarations
使用名称name作为C++命名空间的所有声明
-r host[:port[:uid:pwd]]connect via proxy host, port, and proxy credentials
连接通过代理主机,端口和代理凭证
-sdon't generate STL code (no std::string and no std::vector)
不要生成STL代码
-t fileuse type map file instead of the default file typemap.dat
使用type map文件代替默认的typemap.dat
-udon't generate unions
不要生成联合
-vverbose output
详细输出
-walways wrap response parameters in a response struct
总是封装在一个响应结构的响应参数
-Wsuppress warnings
禁止警告
-xdon't generate _XML any/anyAttribute extensibility elements
-ygenerate typedef synonyms for structs and enums
-z1compatibility with 2.7.6e: generate pointer-based arrays
-z2compatibility with 2.7.15: qualify element/attribute referenced members
-_don't generate _USCORE (replace with UNICODE _x005f)
-?print help information
打印帮助信息

使用: soapcpp2 [-1|-2] [-C|-S] [-T] [-L] [-a] [-b] [-c] [-d path] [-e] [-f N] [-h] [-i] [-I path;path;...] [-k] [-l] [-m] [-n] [-p name] [-s] [-t] [-u] [-v] [-w] [-x] [-y] [infile]
soapcpp2 选项

OptionDescription
-1Use SOAP 1.1 namespaces and encodings (default)
-2Use SOAP 1.2 namespaces and encodings
-CGenerate client-side code only
仅生成客户端代码
-SGenerate server-side code only
仅生成服务器端代码
-TGenerate server auto-test code
生成服务器自动测试代码
-LDo not generate soapClientLib/soapServerLib
不要生成soapClientLib/soapServerLib
-aUse value of SOAPAction HTTP header to dispatch method at server side
在服务器端使用SOAPAction HTTP头的值去调度方法
-bserialize byte arrays char[N] as string
-cGenerate pure C code
生成纯C代码
-d < path >Save sources in directory specified by < path >
保存源代码到指定的目录<path>里
-eGenerate SOAP RPC encoding style bindings
-f NFile split of N XML serializer implementations per file
-hPrint a brief usage message
打印一个简短的用法消息
-iGenerate service proxies and objects inherited from soap struct
从SOAP结构生成服务代理和继承的对象
-jGenerate C++ service proxies and objects that can share a soap struct
-I < path >Use < path > (or paths separated with `:') for #import
-kgenerate data structure walkers (experimental)
-lGenerate linkable modules (experimental)
-mGenerate Matlabtm code for MEX compiler
-nWhen used with -p, enables multi-client and multi-server builds:
 Sets compiler option WITH_NONAMESPACES, see Section 9.11
 Saves the namespace mapping table with name < name > _namespaces instead of namespaces
 Renames soap_serve() into < name > _serve() and soap_destroy() into < name > _destroy()
-p < name >Save sources with file name prefix < name > instead of "soap"
保存源代码文件的前缀名<name>代替"soap"
-q < name >Use name for the C++ namespace of all declarations
使用<name>做为C++所有声明的命名空间
-sGenerates deserialization code with strict XML validation checks
生成具有严格的XML验证检查的反序列化代码
-tGenerates code to send typed messages (with the xsi:type attribute)
-uuncomment comments in WSDL/schema output by suppressing XML comments
-vDisplay version info
显示版本信息
-w
Do not generate WSDL and schema files
不生成WSDL和schema文件
-xDo not generate sample XML message files
不生成示例XML消息文件
-yinclude C/C++ type access information in sample XML messages

下面在VC2008中测试使用:
1.新建基于对话框工程;
2.工程目录下新建文件夹,名称为:CalcSoap,拷贝"...\gsoap-2.8\gsoap"目录下的stdsoap2.h、stdsoap2.cpp到此文件夹;
3.在命令提示符下,输入以下内容:

wsdl2h.exe -t ..\..\typemap.dat -s -o "E:\Project\TestgSOAP\CalcSoap\calc.h" http://www.genivia.com/calc.wsdl 

soapcpp2.exe -C -L -j -d "E:\Project\TestgSOAP\CalcSoap" -w -x "E:\Project\TestgSOAP\CalcSoap\calc.h" 


将soapcalcProxy.h、stdsoap2.h、soapC.cpp、soapcalcProxy.cpp、stdsoap2.cpp加入到工程,右键选择soapC.cpp、soapcalcProxy.cpp、stdsoap2.cpp,点击"属性",如下图所示:

去掉预编译头,如下图所示:


在对话框实现文件,添加以下头文件:

# include "..\CalcSoap\soapcalcProxy.h"
# include "..\CalcSoap\calc.nsmap"

添加个测试按钮,按钮事件如下:

void CTestgSOAPDlg : :OnBnClickedButton1()
{
    calcProxy service;
     double result = 5. 0;
     if (service.add( 1. 0, 2. 0, result) == SOAP_OK)
    {
        TRACE(_T( "%lf"), result);
    } 
     else
    {
        TRACE(_T( "%lf"), result);
    }
    service.destroy();
}
调试运行成功。下面测试一下错误日志输出:
void CTestgSOAPDlg : :OnBnClickedButton1()
{
    calcProxy service;
     double result = 5. 0;
     if (service.div( 1. 0, 0. 0, result) == SOAP_OK)
    {
        TRACE(_T( "%lf"), result);
    } 
     else
    {
        TRACE( "%s", service.soap_fault_string()); //这里不要加_T宏
    }
    service.destroy(); //记得销毁
}
除以0的时候,就不会返回 SOAP_OK ,打印出失败信息,如下图所示:
 
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值