1.创建win32控制台工程webservice-client;
2.将stdsoap2.h、stdsoap2.cpp、interface.nsmap、soapH.h、soapClient.cpp、soapStub.h、soapC.cpp、stringEx.h拷贝到webservice-client目录下,添加到工程(使用预编译头->”否”);
3.新增webservice-client/interfaceFunc.h、webservice-client/interfaceFunc.cpp;
#ifndef _INTERFACEFUNC_H
#define _INTERFACEFUNC_H
#include "stdsoap2.h"
#include "soapStub.h"
int func_add(const char* pszServer, int num1, int num2, int *nSum);
#endif
#include "interfaceFunc.h"
int func_add(const char* pszServer, int num1, int num2, int *nSum)
{
int nResult = 0;
struct soap stAddSoap;
soap_init(&stAddSoap);
//soap_set_namespaces(&stAddSoap, add_namespaces);
// 该函数是客户端调用的主要函数,后面几个参数和interface.h中声明的一样,前面多了个参数,函数名是接口函数名ns__add前面加上soap_call_
soap_call_ns__add(&stAddSoap, pszServer, "", num1, num2, nSum);
if (stAddSoap.error)
{
std::cout << "soap error:" << stAddSoap.error << ", "
<< *soap_faultcode(&stAddSoap) << ", " << *soap_faultstring(&stAddSoap)
<< std::endl;
nResult = stAddSoap.error;
}
soap_end(&stAddSoap);
soap_done(&stAddSoap);
return nResult;
}
4.编写主线程入口函数_tmain;
// webservice-client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stringEx.h"
#include "soapH.h"
#include "interfaceFunc.h"
#include "interface.nsmap"
int _tmain(int argc, _TCHAR* argv[])
{
int nNumber1 = 0, nNumber2 = 0, nSum = 0;
char *pszServer = "http://localhost:8101";
if (argc < 3)
{
std::cout << "usage:" << argv[0] << " num1 num2" << std::endl;
return 0;
}
char *pszNum = THCAR2char(argv[1]);
nNumber1 = atoi(pszNum);
delete []pszNum;
pszNum = THCAR2char(argv[2]);
nNumber2 = atoi(pszNum);
delete []pszNum;
pszNum = NULL;
int nResult = func_add(pszServer, nNumber1, nNumber2, &nSum);
if (nResult != 0)
{
std::cout << "soap error:" << nResult << std::endl;
}
else
{
std::cout << nNumber1 << " + " << nNumber2 << " = " << nSum << std::endl;
}
system("pause");
return 0;
}
5.带参数调试运行。