客户端代码生成
-
下载 gsoap
gsoap 下载地址 -
生成头文件
执行以下命令, 根据 wsdl 路径生成 头文件:
wsdl2h -s -o xxxx.h http://xxxxxxx.asmx?wsdl
-
生成代理类
①.从 gsoap 目录拷贝 typemap.dat 到 bin 文件夹
②.在 typemap.dat 末尾增加以下内容,以更好支持中文
xsd__string = | std::wstring | wchar_t*
③.执行以下命令,生成代理类:
soapcpp2 -d gsoap -i -C -x -IF:\gsoap_2.8.106\gsoap-2.8\gsoap\import xxxx.h
④.从 gsoap 目录拷贝 stdsoap2.cpp、stdsoap2.h 到生成的文件夹
Qt 项目使用
- 添加以上生成的文件到当前项目
- pro 中增加一下内容:
LIBS+= D:\Qt\Qt5.14.2\Tools\mingw730_32\i686-w64-mingw32\lib\libws2_32.a
- 主程序调用即可
#include <QCoreApplication>
#include "updataServiceSoap.nsmap"
#include "soapupdataServiceSoapProxy.h"
#include <QDebug>
using namespace std;
string wstring2string(wstring wstr)
{
string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
updataServiceSoapProxy * updateservice = new updataServiceSoapProxy("http://192.168.1.107:190/updataService.asmx");
_ns1__HelloWorld helloWorld;
_ns1__HelloWorldResponse helloWorldResponse;
wchar_t wt[] = L"你好,世界abchd1234";
helloWorld.helloworld = wt;
updateservice->HelloWorld(&helloWorld, helloWorldResponse);
wstring wstr = helloWorldResponse.HelloWorldResult;
string res = wstring2string(wstr);
cout << res;
return a.exec();
}