gSoap 创建Web Service
1.新建控制台程序CalculatorService,并新建头文件Calculator.h
typedef double xsd__double;
int ns__add(xsd__double a, xsd__double b, xsd__double &result);
int ns__sub(xsd__double a, xsd__double b, xsd__double &result);
int ns__sqrt(xsd__double a, xsd__double &result);
2.运行soapcpp2.exe生成相关文件
3.配置CalculatorService工程:加入wsock32.lib库
4. CalculatorService主程序代码
int main()
{
structsoap soap;
int m, s;
soap_init(&soap);
m = soap_bind(&soap, NULL, 18888,100);
if (m< 0)
soap_print_fault(&soap,stderr);
else
{
fprintf(stderr, "Socket connection successful: master socket =%d\n", m);
for(int i = 1; ; i++)
{
s = soap_accept(&soap);
if(s < 0)
{
soap_print_fault(&soap,stderr);
break;
}
fprintf(stderr, "%d: accepted connection from IP=%d.%d.%d.%dsocket=%d", i,
(soap.ip >>24)&0xFF, (soap.ip >> 16)&0xFF, (soap.ip >> 8)&0xFF,soap.ip&0xFF, s);
if (soap_serve(&soap) != SOAP_OK) soap_print_fault(&soap, stderr);
fprintf(stderr, "request served\n");
soap_destroy(&soap);
soap_end(&soap);
}
}
soap_done(&soap);
return 0;
}
//实现相应的函数声明
int ns__add(struct soap *soap, doublea, double b, double&result)
{
result = a + b;
returnSOAP_OK;
}
int ns__sub(struct soap *soap, doublea, double b, double&result)
{
result = a - b;
returnSOAP_OK;
}
int ns__sqrt(struct soap *soap, doublea, double &result)
{
if (a>= 0)
{
result = sqrt(a);
returnSOAP_OK;
}
else
returnsoap_receiver_fault(soap, "Square root ofnegative number", "I can only takethe square root of a non-negative number");
}
5.启动Service
在浏览器地址栏输入
显示的XML为
需要设置soap.fget:
soap_init(&soap);
soap.fget = http_get;
m = soap_bind(&soap, NULL, 18888,100);
int http_get(struct soap *soap)
{
FILE*fd = NULL;
fd = fopen("ns.wsdl","rb");
if (!fd)
{
return404;
}
soap->http_content = "text/xml";
soap_response(soap,SOAP_FILE);
for(;;)
{
size_t r =fread(soap->tmpbuf,1, sizeof(soap->tmpbuf),fd);
if(!r)
{
break;
}
if(soap_send_raw(soap, soap->tmpbuf, r))
{
break;
}
}
fclose(fd);
soap_end_send(soap);
returnSOAP_OK;
}
,这时再刷新http://localhost:18888/,正确获得XML
6.创建客户端:CalculatorClient并吧soapcpp2.exe生成的文件拷到目录下。
客户端主程序代码
int add( const char* server, double num1, doublenum2, double &sum )
{
structsoap add_soap;
intresult = 0;
soap_init(&add_soap);
soap_call_ns__add( &add_soap, server,"", num1, num2, sum );
if(add_soap.error)
{
printf("soaperror:%d,%s,%s/n", add_soap.error, *soap_faultcode(&add_soap),*soap_faultstring(&add_soap) );
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
returnresult;
}
int sub(const char* server, double num1, doublenum2, double &sum)
{
structsoap add_soap;
intresult = 0;
soap_init(&add_soap);
soap_call_ns__sub( &add_soap, server,"", num1, num2, sum );
if(add_soap.error)
{
printf("soaperror:%d,%s,%s/n", add_soap.error, *soap_faultcode(&add_soap),*soap_faultstring(&add_soap) );
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
returnresult;
}
int Sqrt(const char* server, double num1, double&sum)
{
structsoap add_soap;
intresult = 0;
soap_init(&add_soap);
soap_call_ns__sqrt( &add_soap,server, "", num1,sum );
if(add_soap.error)
{
printf("soaperror:%d,%s,%s/n", add_soap.error, *soap_faultcode(&add_soap),*soap_faultstring(&add_soap) );
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
returnresult;
}
int main()
{
intresult = -1;
char*server="http://localhost:18888";
doublenum1 = 5;
doublenum2 = 6;
doublesum = 0;
result = add(server, num1, num2,sum);
if(result != 0)
{
printf("soaperr,errcode = %d/n", result);
}
else
{
printf("%.2f+ %.2f =%.2f \n", num1, num2, sum );
}
result = sub(server, num1, num2,sum);
if(result != 0)
{
printf("soaperr,errcode = %d/n", result);
}
else
{
printf("%.2f -%.2f =%.2f \n", num1, num2, sum);
}
result = Sqrt(server, num1, sum);
if(result != 0)
{
printf("soaperr,errcode = %d/n", result);
}
else
{
printf("sqrt(%.2f) =%.2f \n", num1, sum );
}
system("pause");
return 0;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。