这篇文章主要介绍了C++实现一个简单的SOAP客户端,在C++中,一般使用gSOAP来实现客户端、服务端,下面一起进入文章了解具体内容,需要的朋友可以参考一下
正文
C++实现一个简单的SOAP客户端
1、简介
在中,一般使用g来实现、服务端。然而,对小项目来说gSOAP太大了,也不太方便。我们完全可以自己实现SOAP协议,毕竟SOAP协议的本质就是:Http协议+XML。
文章介绍了gSOAP的使用,本文就以它的服务端为例,实现一个SOAP客户端。这里需要使用下面两个库:
-
cpp-httplib:一个 C++11 单文件头文件跨平台、多线程“阻塞”的HTTP/HTTPS库,使用时只需在项目中包含httplib.h文件。 -
tinyxml2:tinyXML-2 是一个简单、小巧、高效的 C++ XML 解析器,可以轻松集成到其他程序中,用来代替tinyxml,使用时只需在项目中包含tinyxml2.cpp和tinyxml2.h文件。
2、实现客户端
一个SAOP客户端的主要工作流程有3步:构建请求数据的xml、执行Http协议的POST方法、解析响应数据的xml。
2.1 准备xml文件
准备请求数据、响应数据的xml文件,请求数据的xml文件在项目中作为模板使用,响应数据的xml文件仅用于开发参考不是项目必须的文件。
请求数据的xml内容如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://tempuri.org/ns.xsd"> <SOAP-ENV:Body> <ns:add> <a>0</a> <b>0</b> </ns:add> </SOAP-ENV:Body></SOAP-ENV:Envelope> |
响应数据的xml内容如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://tempuri.org/ns.xsd"> <SOAP-ENV:Body> <ns:addResponse> <result>0.0</result> </ns:addResponse> </SOAP-ENV:Body></SOAP-ENV:Envelope> |
2.2 引入库文件
头文件引用如下:
|
1
2
3
4
5
6
|
#include "httplib.h"#include"tinyxml2.h" #include <iostream>#include <string>using namespace std;using namespace tinyxml2; |
项目文件如下:

2.3 构建请求数据的xml
使用tinyxml的文档对象加载xml文件,设置文档对象的节点内容,然后返回xml内容,
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
string CreateReqXml_Add(int a, int b) { tinyxml2::XMLDocument doc; doc.LoadFile("addReqXML.xml"); tinyxml2::XMLElement* pNode = doc.FirstChildElement("SOAP-ENV:Envelope") ->FirstChildElement("SOAP-ENV:Body") ->FirstChildElement("ns:add"); pNode->FirstChildElement("a")->SetText(a); pNode->FirstChildElement("b")->SetText(b); XMLPrinter printer; doc.Print(&printer); return printer.CStr();} |
2.4 执行Http协议的POST方法
构建一个httplib的客户端对象,直接执行POST方法,
代码如下:
|
1
2
3
4
5
|
int a = 12;int b = 13;string strdata = CreateReqXml_Add(a, b); httplib::Client cli("http://localhost:8080"); auto res = cli.Post("/", strdata, "text/xml; charset=utf-8"); |
注:httplib内部对socket使用了线程锁,可以在多线程中使用一个客户端对象执行Http方法。
2.5 解析响应数据的xml
根据Http方法返回的Result对象判断方法是否成功,Result对象有operator bool() const { return res_ != nullptr; }重载可以直接判断,
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
|
if (res){ cout << res->status << endl; cout << res->get_header_value("Content-Type") << endl; cout << res->body << endl; cout << "Result:" << ParseResXml_Add(res->body) << std::endl;}else{ cout << "error code: " << res.error() << std::endl;} |
解析响应数据xml的方法如下:
|
1
2
3
4
5
6
7
8
9
10
|
string ParseResXml_Add(string xmlStr) { tinyxml2::XMLDocument doc; doc.Parse(xmlStr.c_str(),xmlStr.length()); string result= doc.FirstChildElement("SOAP-ENV:Envelope") ->FirstChildElement("SOAP-ENV:Body") ->FirstChildElement("ns:addResponse") ->FirstChildElement("result")->GetText(); return result;} |
3、测试客户端
先启动服务端,在启动客户端的调试,结果如下:
|
1
2
3
4
5
6
|
200text/xml; charset=utf-8<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://tempuri.org/ns.xsd"><SOAP-ENV:Body><ns:addResponse><result>25</result></ns:addResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>Result:25 |
到此这篇关于C++实现一个简单的SOAP客户端的文章就介绍到这了,更多相关C++实现SOAP客户端内容请搜索米米素材网以前的文章或继续浏览下面的相关文章希望大家以后多多支持米米素材网!
附件:
原文链接:https://www.cnblogs.com/timefiles/p/HttplibTtinyxml2.html

发表评论