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;
}
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;
}