gSOAP C++ Webservice 客户端实现
gSOAP下载
gSOAP下载
目前最新版本为:gsoap_2.8.66
gSOAP生成c++客户端代码文件
解压gsoap_2.8.66,在gsoap-2.8\gsoap\bin\win32下创建文件mytypemap.dat
(解决中文乱码需要创建文件mytypemap.dat同时添加内容:xsd__string = | std::wstring | wchar_t*)
gsoap-2.8\gsoap\bin\win32路径下控制台执行以下命令
wsdl2h -o Parkinginfo.h -t mytypemap.dat http://XXX.XXX.XXX.XXX:XXX/??????/services/????wsdl
soapcpp2 -c Parkinginfo.h
创建工程
VS2010创建win32控制台工程,不选预编译
拷贝ParkingHttpBinding.nsmap/soapClient.cpp/soapC.cpp/soapStub.h/soapH.h共4个文件到工程路径下
同时拷贝gsoap-2.8\gsoap路径下stdsoap2.cpp/stdsoap2.h文件到工程路径下
以下是工程代码
#include "stdafx.h"
#include <iomanip>
#include "soapH.h" // include all interfaces (library and generated)
#include "ParkingHttpBinding.nsmap" // import the generated namespace mapping table
int _tmain(int argc, _TCHAR* argv[])
{
_ns1__Parkinginfo parkinginfo;//入参
_ns1__ParkinginfoResponse parkinginfoResponse;//出参
#if 0 //上报实时车位信息
parkinginfo.xtlb = _T("01");
parkinginfo.jkxlh = _T("XXXX");
parkinginfo.XmlString = _T("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml><head><bh>XXXX</bh><mc>XXXX</mc><scsj>2018-05-24 14:08:48</scsj><version>1.0</version></head><data><jccsjid>123456789</jccsjid><hphm>粤A11120</hphm><hpzl>02</hpzl><fs>1</fs><crkbh>01</crkbh><csys>A</csys><tp></tp><tztp></tztp><tgsj>2018-05-24 14:08:48</tgsj><sjzt>01</sjzt><id></id></data></xml>");
#else //定时刷新停车场数据
parkinginfo.xtlb = _T("02");
parkinginfo.jkxlh = _T("XXXX");
parkinginfo.XmlString = _T("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml><head><bh>XXXX</bh><mc>XXXX</mc><cwzs>200</cwzs><scsj>2018-05-24 14:08:48</scsj><version>1.0</version></head><data><ztsjid>123456789</ztsjid><tcczt>01</tcczt><scsj>2018-05-24 14:08:48</scsj><sycws>100</sycws><gdcws>50</gdcws><sygdcws>30</sygdcws><lscws>150</lscws><sylscws>70</sylscws></data></xml>");
#endif
struct soap soap;
soap_init(&soap);
soap_set_mode(&soap, SOAP_C_UTFSTRING);
setlocale(LC_ALL, "");
if (soap_call___ns1__Parkinginfo(&soap, NULL, NULL, &parkinginfo, parkinginfoResponse) == SOAP_OK)
printf_s("soap = %S", parkinginfoResponse.out);
else // an error occurred
soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream
soap_destroy(&soap); // delete deserialized class instances (for C++)
soap_end(&soap); // remove deserialized data and clean up
soap_done(&soap); // detach the gSOAP context
getchar();
return 0;
}
需要注意的是:如果使用IP:PORT形式的URL地址需要确定soapClient.cpp中soap_endpoint = "http://xxx.xxx.xxx.xxx:port/????/services/????"中的端口是不是存在。
网友评论