C++ (使用gsoap)调用 WCF服务
背景:c++通过使用gsoap调用wcf发布的basicHttpBinding服务。
(转载请注明来源:cnblogs coder-fang)
- 创建WCF服务并启动,这里不多说,请参考 https://www.cnblogs.com/coder-fang/p/6594964.html ,确定服务接口已发布,浏览器可访问wsdl,如下(我创建的示例服务提供basicHttpBinding 和 netTcpBinding两种方式):
- 下载gSoap工具,下载地址:http://sourceforge.net/projects/gsoap2 ,解压至指定目录,我这里放在 D:\Program Files (x86)\gsoap-2.8
- 进入 D:\Program Files (x86)\gsoap-2.8\gsoap\bin\win32 ,创建 wsmap.dat,文件里输入: xsd__string = | std::wstring | wchar_t*
- 在此目录下运行两个cmd命令:
- 运行如下命令生成 service.h
wsdl2h.exe -s -t wsmap.dat -o service.h http://localhost:9999/?wsdl
- 运行如下命令,生成相关代理类:
soapcpp2.exe -C -I"D:\Program Files (x86)\gsoap-2.8\gsoap\import" -L -i -x -1 service.h
- 此时目录会多出几个c++文件:
- 运行如下命令生成 service.h
- 创建C++控制台程序,将以上文件复制到工程中,并同时复制 stdsoap2.h 与 stdsoap2.cpp (在gsoap\import目录下),使工程包含各文件,同时将工程设置为 不使用预编译:
- 主函数文件代码如下:
// C_Client.cpp: 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdlib.h> #include <iostream> #include "soapBasicHttpBinding_USCOREIServiceProxy.h" #include "BasicHttpBinding_USCOREIService.nsmap" #pragma warning(disable : 4996) std::string WString2String(const std::wstring& ws) { std::string strLocale = setlocale(LC_ALL, ""); const wchar_t* wchSrc = ws.c_str(); size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1; char *chDest = new char[nDestSize]; memset(chDest, 0, nDestSize); wcstombs(chDest, wchSrc, nDestSize); std::string strResult = chDest; delete[]chDest; setlocale(LC_ALL, strLocale.c_str()); return strResult; } // string => wstring std::wstring String2WString(const std::string& s) { std::string strLocale = setlocale(LC_ALL, ""); const char* chSrc = s.c_str(); size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1; wchar_t* wchDest = new wchar_t[nDestSize]; wmemset(wchDest, 0, nDestSize); mbstowcs(wchDest, chSrc, nDestSize); std::wstring wstrResult = wchDest; delete[]wchDest; setlocale(LC_ALL, strLocale.c_str()); return wstrResult; } int main() { const char* addr = "http://localhost:9999/Service"; BasicHttpBinding_USCOREIServiceProxy proxy(addr, SOAP_C_UTFSTRING); _ns1__GetUsersResponse rsp; _ns1__GetUsers getData; wchar_t start[] = L"z";//返回以 z 开头的名称 getData.startWith = start; if (proxy.GetUsers(&getData, rsp) == SOAP_OK) { for (int i = 0; i < rsp.GetUsersResult->__sizestring; i++) { printf("%s \n", WString2String(*rsp.GetUsersResult->string++).c_str()); } } else { proxy.soap_stream_fault(std::cerr); } proxy.destroy(); return 0; }
// C_Client.cpp: 定义控制台应用程序的入口点。
//#include "stdafx.h"#include <stdlib.h>
#include <iostream>#include "soapBasicHttpBinding_USCOREIServiceProxy.h"
#include "BasicHttpBinding_USCOREIService.nsmap"
#pragma warning(disable : 4996)std::string WString2String(const std::wstring& ws)
{
std::string strLocale = setlocale(LC_ALL, "");
const wchar_t* wchSrc = ws.c_str();
size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
char *chDest = new char[nDestSize];
memset(chDest, 0, nDestSize);
wcstombs(chDest, wchSrc, nDestSize);
std::string strResult = chDest;
delete[]chDest;
setlocale(LC_ALL, strLocale.c_str());
return strResult;
}
// string => wstring
std::wstring String2WString(const std::string& s)
{
std::string strLocale = setlocale(LC_ALL, "");
const char* chSrc = s.c_str();
size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1;
wchar_t* wchDest = new wchar_t[nDestSize];
wmemset(wchDest, 0, nDestSize);
mbstowcs(wchDest, chSrc, nDestSize);
std::wstring wstrResult = wchDest;
delete[]wchDest;
setlocale(LC_ALL, strLocale.c_str());
return wstrResult;
}
int main()
{
const char* addr = "http://localhost:9999/Service";
BasicHttpBinding_USCOREIServiceProxy proxy(addr, SOAP_C_UTFSTRING);
_ns1__GetUsersResponse rsp;
_ns1__GetUsers getData;
wchar_t start[] = L"z";//返回以 z 开头的名称
getData.startWith = start;
if (proxy.GetUsers(&getData, rsp) == SOAP_OK)
{
for (int i = 0; i < rsp.GetUsersResult->__sizestring; i++)
{
printf("%s \n", WString2String(*rsp.GetUsersResult->string++).c_str());
}
}
else
{
proxy.soap_stream_fault(std::cerr);
}
proxy.destroy();
return 0;
} - 运行效果:
- 目前只试过使用http绑定模式,tcp绑定暂时未通过,正在尝试中。