C++实现一个简单的SOAP客户端

2022-07-21,,

目录
  • 1、简介
  • 2、实现客户端
    • 2.1 准备xml文件
    • 2.2 引入库文件
    • 2.3 构建请求数据的xml
    • 2.4 执行http协议的post方法
    • 2.5 解析响应数据的xml
  • 3、测试客户端

    1、简介

    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 文件。

    2、实现客户端

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

    2.1 准备xml文件

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

    请求数据的xml内容如下:

    <?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内容如下:

    <?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 引入库文件

    头文件引用如下:

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

    项目文件如下:

    2.3 构建请求数据的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();
    }
    
    

    2.4 执行http协议的post方法

    构建一个httplib的客户端对象,直接执行post方法,

    代码如下:

    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; }重载可以直接判断,

    代码如下:

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

    3、测试客户端

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

    200
    text/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客户端内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

    附件:

    使用项目文件(包含服务端程序) 提取码: 4qpm
    cpp-httplib-master-20210826备份 提取码: n4yg
    tinyxml2-master-20210921备份 提取码: yjv8

    《C++实现一个简单的SOAP客户端.doc》

    下载本文的Word格式文档,以方便收藏与打印。