win7 webservice
番茄系统家园 · 2021-08-05 08:40:08

环境:
QT版本:
:2.8
编译器:
系统环境:
Demo地址:
##前言
工具包是用于SOAP和服务以及通用C/C++XML数据绑定的C和C++软件开发工具包。该工具包分析WSDL和XML模式(单独或作为组合集),并将XML模式类型和消息传递win7 webservice协议映射为易于使用且高效的C和C++代码。它还支持通过自动生成XML序列化代码和WSDL规范将(传统)C和C++应用程序作为服务公开。或者,您可以简单地使用它自动将XML转换为C和C++数据。该工具包支持生成带有或不带STL的纯ANSIC或C++的选项。
##1.下载GSoap
Gsoap下载地址:https://sourceforge.net/projects/gsoap2/files
##2.编写webservice所需头文件
假设头文件命名为myStr.h
//gsoapnsservicename:myStr//gsoapnsservicestyle:rpc//gsoapnsservicenamespace:http://localhost:8088/myStr.wsdl//gsoapnsservicelocation:http://localhost:8088//gsoapnsschemanamespace:urn:myStrintns__myStrCat(std::stringstr1In,std::stringstr2In,std::stringparamOut);
##3.导出源文件
使用soapcpp2.exe导出所需源文件
执行命令:
soapcpp2myStr.h
比较重要的文件列表:
myStr.nsmap:要用一个.cpp文件include,不然编译报错
soapC.cpp:服务器和客户端都需要
soapClient.cpp:客户端用到
soapClientLib.cpp:可以不用
soapH.h:头文件
soapServer.cpp:服务器用到
soapServerLib.cpp
soapStub.h:可以不用
myStr.wsdl:wsdl文件
##4.建立服务端工程
webservice做为服务端需要的Gsoap文件有8个:
###1.生成的文件
myStr.nsmap、soapC.cpp、soapH.h、soapServer.cpp、soapStub.h、myStr.wsdl
###2.GSoap文件
stdsoap2.h、stdsoap2.cpp。位于gsoap-2.8\gsoap目录中
核心代码:
#ifndefWEBSERVICESERVERLIB_H#defineWEBSERVICESERVERLIB_H#includeQObjectclasswebserviceServerLib:publicQObject{Q_OBJECTpublic:explicitwebserviceServerLib(intport=8088,QObject*parent=0);~webserviceServerLib();voidlisten();signals:publicslots:};#endif//WEBSERVICESERVERLIB_H#include"webserviceserverlib.h"#include"myStr.nsmap"//服务端与客户端必须包含的文件#includeQDebug#includeQtConcurrentstructsoapws_service;inthttp_get(structsoap*soapObject){FILE*fd=NULL;//wscplus.wsdl是执行soapcpp2.exe命令时生成的。把他拷贝到了当前目录下。fd=fopen("myStr.wsdl","rb");//openWSDLfiletocopyif(!fd){return404;//returnHTTPnotfounderror}soapObject-http_content="text/xml";//HTTPheaderwithtext/xmlcontentsoap_response(soapObject,SOAP_FILE);for(;;){size_tr=fread(soapObject-tmpbuf,1,sizeof(soapObject-tmpbuf),fd);if(!r){break;}if(soap_send_raw(soapObject,soapObject-tmpbuf,r)){break;//cannotsend,butlittlewecandoaboutthats}}fclose(fd);soap_end_send(soapObject);returnSOAP_OK;}webserviceServerLib::webserviceServerLib(intport,QObject*parent){intiRet=-1;soap_init(ws_service);soap_set_mode(ws_service,SOAP_C_UTFSTRING);ws_service.fget=http_get;soap_set_namespaces(ws_service,namespaces);//bind端口返回SOCKET套接字-雷同socket套接口函数服务器监听过程iRet=soap_bind(ws_service,"0.0.0.0",port,100);if(iRet0){soap_print_fault(ws_service,stderr);qDebug()stderr;return;}else{qDebug()tr("WebserviceServerinitOK.port:%1").arg(port);}QtConcurrent::run(this,webserviceServerLib::listen);}webserviceServerLib::~webserviceServerLib(){}voidwebserviceServerLib::listen(){intiRet=-1;while(true){iRet=soap_accept(ws_service);if(iRet0){soap_print_fault(ws_service,stderr);qDebug()stderr;}else{qDebug()tr("receiveIP:%1").arg(ws_service.host);fprintf(stderr,"Socketconnectionsuccessful:slavesocket=%d
",iRet);}soap_serve(ws_service);//provideservicesoap_end(ws_service);//endservice}}intns__myStrCat(structsoap*mysoap,std::stringstr1In,std::stringstr2In,std::stringresultOut){qDebug()str1In.c_str()""str2In.c_str();resultOut=str1In+str2In;//jsonreturnSOAP_OK;}
###3.运行结果:
这里我们发现直接访问,返回的是404。这是因为可执行程序目录中没有找到myStr.wsdl
将myStr.wsdl放到可执行程序目录,重新执行。得到正确的返回。
Reference:
http://www.cs.fsu.edu/~engelen/soap.html
觉得文章对你有帮助,可以扫描二维码捐赠给博主,谢谢!
如需转载请标明出处:http://blog.csdn.net/itas109

