C++实现一个SOAP客户端

简介

在C++中,一般使用gSOAP来实现客户端、服务端。然而,对小项目来说gSOAP太大了,也不太方便。我们完全可以自己实现SOAP协议,毕竟SOAP协议的本质就是:Http协议+XML

文章C++中gSOAP的使用介绍了gSOAP的使用,本文就以它的服务端为例,实现一个SOAP客户端。这里需要使用下面两个库:

  • cpp-httplib:一个 C++11 单文件头文件跨平台、多线程“阻塞”的 HTTP/HTTPS 库,使用时只需在项目中包含httplib.h文件
  • tinyxml2:tinyXML-2 是一个简单、小巧、高效的 C++ XML 解析器,可以轻松集成到其他程序中,用来代替tinyxml,使用时只需在项目中包含 tinyxml2.cpp 和 tinyxml2.h 文件

库的使用方法可以参考以下文章或github:

实现客户端

一个SAOP客户端的主要工作流程有3步:构建请求数据的xml、执行Http协议的POST方法、解析响应数据的xml。

准备xml文件

准备请求数据、响应数据的xml文件,请求数据的xml文件在项目中作为模板使用,响应数据的xml文件仅用于开发参考不是项目必须的文件。
请求数据的xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="//schemas.xmlsoap.org/soap/envelope/" 
    xmlns:SOAP-ENC="//schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="//www.w3.org/2001/XMLSchema" 
    xmlns:ns="//tempuri.org/ns.xsd">
  <SOAP-ENV:Body>
    <ns:add>
      <a>0</a>
      <b>0</b>
    </ns:add>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

响应数据的xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="//schemas.xmlsoap.org/soap/envelope/"
    xmlns:SOAP-ENC="//schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="//www.w3.org/2001/XMLSchema"
    xmlns:ns="//tempuri.org/ns.xsd">
 <SOAP-ENV:Body>
  <ns:addResponse>
   <result>0.0</result>
  </ns:addResponse>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

引入库文件

头文件引用如下:

#include "httplib.h"
#include"tinyxml2.h" 
#include <iostream>
#include <string>
using namespace std;
using namespace tinyxml2;

项目文件如下:

构建请求数据的xml

使用tinyxml的文档对象加载xml文件,设置文档对象的节点内容,然后返回xml内容,代码如下:

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();
}

执行Http协议的POST方法

构建一个httplib的客户端对象,直接执行POST方法,代码如下:

int a = 12;
int b = 13;
string strdata = CreateReqXml_Add(a, b);   
httplib::Client cli("//localhost:8080");   
auto res = cli.Post("/", strdata, "text/xml; charset=utf-8");   

注:httplib内部对socket使用了线程锁,可以在多线程中使用一个客户端对象执行Http方法

解析响应数据的xml

根据Http方法返回的Result对象判断方法是否成功,Result对象有operator bool() const { return res_ != nullptr; }重载可以直接判断,代码如下:

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的方法如下:

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;
}

测试客户端

先启动服务端,在启动客户端的调试,结果如下:

200
text/xml; charset=utf-8
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="//schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="//schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema" xmlns:ns="//tempuri.org/ns.xsd"><SOAP-ENV:Body><ns:addResponse><result>25</result></ns:addResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

Result:25

附件

Tags: