由于在c++中需要调用其他语言提供的webservice,而微软提供的的确不怎么样,下载了gsoap学习一下。
函数名 | GetCigInfo | ||||||
功能 | 返回最新的卷烟品牌列表 | ||||||
原形 | CigInfo[] GetCigInfo(String corpCode) | ||||||
URL |
| ||||||
输入参数 | 名称 | 类型 | 说明 | ||||
corpCode | String | 烟草市公司代码 | |||||
返回值 | 类型 | CigInfo[] 卷烟信息结构体数组,数组长度为卷烟品牌数量 | |||||
说明 | CigInfo结构体说明 | 元素名称 | 类型 | 说明 | |||
cigTypeVersion | String |
| |||||
cigCode | String |
| |||||
cigName | String |
| |||||
barCodeBox | String |
| |||||
barCode | String |
| |||||
wholeSalePrice | Int |
| |||||
retailPrice | Int |
| |||||
scale | Int |
| |||||
mark | String |
| |||||
spec | String |
| |||||
注释 |
这是提供调用的方法说明。
因为返回的有中文,所以先特殊处理一下,先建立一个文件ttt.txt
里面有下面内容:
xsd__string = | std::wstring | wchar_t*
然后执行
wsdl2h -s -o test.h -t ttt.txt service.xml
soapcpp2 -CLx test.h
上面的service.xml是提供服务的wsdl文件
执行上面的两个命令之后,产生了一大堆cpp和h文件,把这些弄到自己的工程上面。同时也需要stdsoap2.cpp和stdsoap2.h文件。
下面是学习的源代码:
#include "soapH.h"
#include "ServiceSoap.NSMAP"
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL,"Chinese-simplified"); // 这个需要设置,否则下面显示汉字不正常
struct soap clientSOAP;
soap_init(&clientSOAP);
const char* server = "http://10.68.23.245:82/service.asmx" ;
_ns1__GetCigInfo t1 ;
//t1.corpCode = new wchar_t[100]; //自己分配内容,自己释放,好像麻烦了点,不知道是否可以用其他方式
t1.corpCode = L"420100";
_ns1__GetCigInfoResponse o1;
if( soap_call___ns3__GetCigInfo(&clientSOAP, server, NULL, &t1, &o1) == SOAP_OK ){
ns1__ArrayOfCigInfo* a1 = o1.GetCigInfoResult ;
for(int i = 0; i < a1->__sizeCigInfo ; i++){ //返回的是数组,根据数组大小来操作
wprintf(L"cigCode----%s\n",a1->CigInfo[i]->cigCode);
wprintf(L"cigName----%s\n",a1->CigInfo[i]->cigName);
printf("scale----%d\n",a1->CigInfo[i]->scale);
}
}else
soap_print_fault(&clientSOAP, stderr);
getchar();
//delete t1.corpCode ; //此处如果不释放,gSoap不知道会否释放
soap_destroy(&clientSOAP);
soap_end(&clientSOAP);
soap_done(&clientSOAP);
return 0;
}