gSoap使用方法

1. 从WSDL得到头文件
wsdl2h -o ayandy.h -n ay http://www.ayandy.com/Service.asmx?WSDL


2. 从头文件得到存根(Stub)源文件
soapcpp2 -i -C -x ayandy.h -ID:\gsoap-2.7\gsoap\import


命令选项注释:


-i 直接使用C++包装类
-x 不要生成一堆看了就恶心的xml
-C 只生成客户端相关代码
-I 指定import路径
3. 建立新项目
把gsoap库里的stdsoap2.cpp文件,以及上一步生成的soapServiceSoapProxy.cpp和soapC.cpp都加入到项 目。


设置加入的这三个文件为不使用预编译头。


4. 编写代码
由于参数及回传的数据都是中文,所有让gSOAP使用UTF8方式传送以防止乱码。


#include <iostream>
#include <string>
#include "soapServiceSoapProxy.h"
#include& nbsp;"ServiceSoap.nsmap" //表忘了名空间定义
using namespace std;
// 宽 字符转UTF8
string EncodeUtf8(wstring in)
{
     string s(in.length()*3+1,' ');
    size_t len = ::WideCharToMultiByte(CP_UTF8, 0,
             in.c_str(), in.length(),
             &s[0], s.length(),
             NULL, NULL);
     s.resize(len);
    return s;
}
// UTF8 转宽字符
wstring DecodeUtf8(string in)
{
     wstring s(in.length(), _T(' '));
    size_t len = ::MultiByteToWideChar(CP_UTF8, 0,
             in.c_str(), in.length(),
             &s[0], s.length());
     s.resize(len);
    return s;
}
int main(int argc, char* argv[])
{
     ServiceSoapProxy gs(SOAP_C_UTFSTRING);
     _ns1__getWeatherbyCityName cityname;
     _ns1__getWeatherbyCityNameResponse resp;
     string strCityName = EncodeUtf8(L"苏州");
     cityname.theCityName = &strCityName;
     cityname.theDayFlag = ns1__theDayFlagEnum__Tomorrow;
    if(gs.getWeatherbyCityName(&cityname, &resp) == SOAP_OK)
     {
         ns1__ArrayOfString *aos = resp.getWeatherbyCityNameResult;
         wcout.imbue( std::locale("chs") ); //指定输出为中文
        for(vector<string>::iterator
                 itr=aos->string.begin(), itr_end = aos->string.end();
                 itr!=itr_end; ++itr)
             wcout << DecodeUtf8(*itr) << endl;
     }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
带gsoap-2.8源码,基于服务器客户端的实例,带自动生成服务客户端代码的批处理程序,及如何使用。带自己学习参考的教程; 0.解压附件,soapInterface.bat所在路径不得含中文 空格 1.新建头文件 取soapInterface.bat文件的同名:soapInterface.h 文件内编写接口,具体说明看附件参考的教程 //gsoap ns service name: gservice //gsoap ns service style: rpc int ns__add(int num1, int num2, int* result ); int ns__sub(int num1, int num2, int* result ); int ns__mult( int num1, int num2, int *result); int ns__divid( int num1, int num2, int *result); 2.从附件内gsoap-2.8包中搜索复制stdsoap2.h,stdsoap2.cpp,soapcpp2.exe, 存放于soapInterface.bat同级目录 3.双击soapInterface.bat运行。生成gClientSoap,gServerSoap两个文件夹,分别复制到服务器工程与客户端工程中使用 4.gClientSoap,gServerSoap两个文件夹内用到的文件功能说明,更多参考附件教程 1)soapC.cpp , soapH.h//soap的序列和反序列代码,它已经包含了soapStub.h 2)soapServer.c ppsoapServerLib.cpp //服务器端代码(纯C代码是soapServer.c soapServerLib.c ),soapServerLib.cpp文件则只是简单地包含soapServer.cpp和soapC.cpp 3)soapClient.cpp soapClientLib.cpp//客户端代码(纯C代码是soapClient.csoapClientLib.c ),soapClientLib.cpp文件则只是简单地包含soapClient.cpp和soapC.cpp 4) soapStub.h // soap的存根文件,定义了我们编写的头文件里对应的远程调用模型 5) add.nsmap //XML服务命名空间 6)服务器端要载入的文件有:soapServer.cpp,soapC.cpp,stdsoap2.cpp; 要包含的文件有:gservice.nsmap,soapH.h; 客户端要输入的文件有: soapClient.cpp,soapC.cpp,stdsoap2.cpp; 要包含的文件有:gservice.nsmap,soapH.h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值