您的位置:首页 > 编程语言 > Qt开发

qt 使用gsoap发布webservice服务与调用webservice服务

2018-01-05 16:56 441 查看
近期工程需要用到,编写了一个简单的例子,遇到了点问题,记录下来并分享让以后的人可以少走弯路

一、服务端

1.首先要下载gsoap开发包http://sourceforge.net/projects/gsoap2/

2.解压并编写自己需要执行的函数头文件mySoap.h

//gsoap ns service name: dataStream
//gsoap ns service namespace: http://localhost/dataStream.wsdl //gsoap ns service location: http://localhost //gsoap ns service executable: dataStream.cgi
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn:dataStream
int ns__dataStream(std::wstring param_in, std::wstring& param_out);
3.使用soapcpp2.exe导出需要的源文件

在下载源码包的gsoap2-8/gsoap/bin/win32目录下有soapcpp2.exe,打开控制台应用程序切换到gsoap2-8/gsoap/bin/win32目录下执行soapcpp2.exe
mySsoap.h生成以下几个文件

dataStream.nsmap

soapC.cpp:服务器和客户端都需要

soapClient.cpp:客户端用到

soapClientLib.cpp

soapH.h

soapServer.cpp:服务器用到

soapServerLib.cpp

soapStub.h

dataStream.dataStream.req.xml


dataStream.dataStream.res.xml

dataStream.nsmap:wsdl
ns.xsd

4.新建服务器工程,添加3生成的部分源文件和库中的2个文件(stdsoap2.h、stdsoap2.cpp,stdsoap2.h
、stdsoap2.cpp在源码包的gsoap-2.8/gsoap)到工程

服务器添加mySsoap.h、soapH.h、soapStub.h、soapServer.cpp、soapC.cpp、stdsoap2.cpp、stdsoap2.h、dataStream.nsmap
等8个文件,pro文件添加这一行
LIBS += -lpthread libwsock32 libws2_32


工程添加以下头文件,qDebug不需要可以不添加

#include "stdsoap2.h"

#include "mySoap.h"

#include "datastream.nsmap"

#include "QDebug"

我只是为了测试就随便把发布webservice的过程写到了构造函数里,正常使用千万不要这么写



#include "service.h"

#include "stdsoap2.h"

#include "mySoap.h"

#include "datastream.nsmap"

#include "QDebug"

Service::Service(QWidget *parent)

: QMainWindow(parent)

{

int m, s;

struct soap add_soap;

soap_init(&add_soap);

//soap_set_namespaces(&add_soap, add_namespaces);


m = soap_bind(&add_soap, "127.0.0.1", 30000, 100);//指定本地端口与ip供客户端调用

if (m < 0)

{

soap_print_fault(&add_soap, stderr);

exit(-1);

}

qDebug()<<"Socket connection successful: master socket =";

qDebug()<< m;

while(true)

{

s = soap_accept(&add_soap);

if (s < 0)

{

    soap_print_fault(&add_soap, stderr);

    exit(-1);

}

fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);

qDebug()<<"Socket connection successful: slave socket =";

qDebug()<< s;

soap_serve(&add_soap);//该句说明该server的服务

soap_end(&add_soap);

}

}

int  ns__dataStream(struct soap* mysoap, std::wstring param_in, std::wstring& param_out)

{

wprintf(L"\nreceived %s\n", param_in.c_str());

param_out = L"中文456 ";


return 0;

}

Service::~Service()

{


}



一、客户端

发布服务时已经做过的操作不用再做了,只要拿来用就行了,建立客户端工程,添加mySsoap.h、soapH.h、soapStub.h、soapClient.cpp、soapC.cpp、stdsoap2.cpp、stdsoap2.h、dataStream.nsmap
等8个文件,pro文件添加这一行
LIBS += -lpthread libwsock32 libws2_32


客户端源码

#include "client.h"

#include "stdsoap2.h"

#include "soapH.h"

#include "datastream.nsmap"

#include "QDebug"

client::client(QWidget *parent)

: QMainWindow(parent)

{

struct soap add_soap;

int result = 0;

soap_init(&add_soap);

std::wstring str_in = L"<message><type>write</type><device_id>12</device_id><point_id>617</point_id><value>0</value></message>";

std::wstring str_out;

// soap_set_namespaces(&add_soap, add_namespaces);

// 该函数是客户端调用的主要函数,后面几个参数和add.h中声明的一样,前面多了3个参数,

// 函数名是接口函数名ns__add前面加上soap_call_

soap_call_ns__dataStream( &add_soap, "127.0.0.1:30000", NULL,str_in, str_out );/*端口ip对应服务器发布的端口ip、函数名字为mySoap.h中的函数名字前面加上soap_call_*/

if(add_soap.error)

{

printf("soap error:%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);

}


client::~client()

{


}

最后放出工程源码地址http://download.csdn.net/download/kaituojimo/10190571,服务端客户端都有,win 10 +qt creator5.9.2开发环境,下载即可编译
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qt qt5 gsoap qt gsoap qt5 soap