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
附件
- 使用項目文件(包含服務端程式) 提取碼: 4qpm
- cpp-httplib-master-20210826備份 提取碼: n4yg
- tinyxml2-master-20210921備份 提取碼: yjv8