原文链接:
http://robot9.me/2014/03/qt%E4%BD%BF%E7%94%A8gsoap%E8%B0%83%E7%94%A8webservice/
Qt并没有SOAP的官方实现,当然你可以使用QtNetwork模块自己写,这里介绍下如何用gSOAP这个库来实现:
1、下载gSOAP(http://sourceforge.net/projects/gsoap2/),解压到某个目录,如D:\gsoap-2.7
2、新建一个目录,如E:\soapClient,命令行进入该目录,运行如下命令:
1
|
D:\gsoap-2.7\gsoap\bin\win32\wsdl2h.exe -I
"D:\gsoap-2.7\gsoap\WS"
-o HelloWorldSOAP.h http:
//xxxxx
:xxxx
/xxxx
.asmx?WSDL
|
其中http://xxxxx:xxxx/xxxx.asmx?WSDL是你的WebService地址,上面命令将生成HelloWorldSOAP.h文件,继续在该目录运行命令:
1
|
D:\gsoap-2.7\gsoap\bin\win32\soapcpp2.exe -I
"D:\gsoap-2.7\gsoap\import"
HelloWorldSOAP.h
|
上面命令将生成好几个文件,先都留着
3、把D:\gsoap-2.7\gsoap\下的stdsoap2.cpp和stdsoap2.h两个文件复制到刚才的E:\soapClient目录,然后开始清理文件,只保留如下文件,其它的都删了
1
2
3
4
5
6
7
8
|
soapC.cpp
soapClient.cpp
soapH.h
soapStub.h
soapxxxxxSoapProxy.h
stdsoap2.cpp
stdsoap2.h
xxxxxSoap.nsmap
|
4、在工程里面添加include
1
2
3
4
5
|
#include "soapC.cpp"
#include "soapClient.cpp"
#include "stdsoap2.cpp"
#include "xxxxxSoap.nsmap"
#include "soapxxxxxSoapProxy.h"
|
5、假设http://xxxxx:xxxx/xxxx.asmx?WSDL里有个方法
1
2
3
4
|
public
string
HelloWorld(
string
msg)
{
return
msg;
}
|
客户端的调用函数如下:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
void
HelloWorldSOAP::hello(QString msg)
{
//初始化SOAP
struct
soap _soap;
soap_init(&_soap);
int
result = 0;
//SOAP返回值,若不为0则表示调用出错
QString errStr =
""
;
//出错提示
QString strRes =
""
;
//结果字符串
//request和response
_ns1__HelloWorld req;
_ns1__HelloWorldResponse res;
//填充参数
std::string msgStr(msg.toStdString());
req.msg = &msgStr;
//调用SOAP
_serverStr =
"http://xxxxx:xxxx/xxxx.asmx"
;
soap_call___ns2__HelloWorld(&_soap, _serverStr,
""
, &req, &res);
if
(_soap.error != 0)
{
//获取错误提示
errStr = QString::fromLocal8Bit(*soap_faultstring(&_soap));
result = _soap.error;
}
else
{
//获取结果字符串
strRes = QString::fromUtf8(res.HelloWorldResult->__any);
}
soap_end(&_soap);
soap_done(&_soap);
//完成
emit ended_HelloWorld(strRes, result != 0, errStr);
}
//信号定义
signals:
void
ended_HelloWorld(QString res,
bool
isError, QString errStr);
|
整个过程就是这样,还是挺方便的!
转载自:http://robot9.me/2014/03/qt%E4%BD%BF%E7%94%A8gsoap%E8%B0%83%E7%94%A8webservice/