目錄
- 簡介
- 實作客戶端
- 準備xml檔案
- 引入庫檔案
- 構建請求資料的xml
- 執行Http協議的POST方法
- 決議回應資料的xml
- 測驗客戶端
- 附件
簡介
在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:
- cpp-httplib庫簡單原理,聽說你還不會開源庫?
- TinyXML2 入門教程
實作客戶端
一個SAOP客戶端的主要作業流程有3步:構建請求資料的xml、執行Http協議的POST方法、決議回應資料的xml,
準備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>
引入庫檔案
頭檔案參考如下:
#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 = https://www.cnblogs.com/timefiles/archive/2021/11/03/CreateReqXml_Add(a, b);
httplib::Client cli("http://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="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
附件
- 使用專案檔案(包含服務端程式) 提取碼: 4qpm
- cpp-httplib-master-20210826備份 提取碼: n4yg
- tinyxml2-master-20210921備份 提取碼: yjv8
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/345517.html
標籤:其他
