C++實現(xiàn)一個簡單的SOAP客戶端
1、簡介
在C++
中,一般使用gSOAP
來實現(xiàn)客戶端、服務(wù)端。然而,對小項目來說gSOAP
太大了,也不太方便。我們完全可以自己實現(xiàn)SOAP
協(xié)議,畢竟SOAP協(xié)議的本質(zhì)就是:Http協(xié)議+XML。
文章C++中g(shù)SOAP的使用介紹了gSOAP的使用,本文就以它的服務(wù)端為例,實現(xiàn)一個SOAP
客戶端。這里需要使用下面兩個庫:
cpp-httplib:
一個 C++11 單文件頭文件跨平臺、多線程“阻塞”的HTTP/HTTPS
庫,使用時只需在項目中包含httplib.h文件。tinyxml2:
tinyXML-2 是一個簡單、小巧、高效的 C++ XML 解析器,可以輕松集成到其他程序中,用來代替tinyxml,使用時只需在項目中包含tinyxml2.cpp
和tinyxml2.h
文件。
2、實現(xiàn)客戶端
一個SAOP客戶端的主要工作流程有3步:構(gòu)建請求數(shù)據(jù)的xml、執(zhí)行Http協(xié)議的POST方法、解析響應(yīng)數(shù)據(jù)的xml。
2.1 準(zhǔn)備xml文件
準(zhǔn)備請求數(shù)據(jù)、響應(yīng)數(shù)據(jù)的xml文件,請求數(shù)據(jù)的xml文件在項目中作為模板使用,響應(yīng)數(shù)據(jù)的xml文件僅用于開發(fā)參考不是項目必須的文件。
請求數(shù)據(jù)的xml內(nèi)容如下:
<?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>
響應(yīng)數(shù)據(jù)的xml內(nèi)容如下:
<?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 構(gòu)建請求數(shù)據(jù)的xml
使用tinyxml
的文檔對象加載xml文件,設(shè)置文檔對象的節(jié)點內(nèi)容,然后返回xml內(nèi)容,
代碼如下:
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 執(zhí)行Http協(xié)議的POST方法
構(gòu)建一個httplib
的客戶端對象,直接執(zhí)行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內(nèi)部對socket使用了線程鎖,可以在多線程中使用一個客戶端對象執(zhí)行Http方法。
2.5 解析響應(yīng)數(shù)據(jù)的xml
根據(jù)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; }
解析響應(yīng)數(shù)據(jù)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、測試客戶端
先啟動服務(wù)端,在啟動客戶端的調(diào)試,結(jié)果如下:
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
到此這篇關(guān)于C++實現(xiàn)一個簡單的SOAP客戶端的文章就介紹到這了,更多相關(guān)C++實現(xiàn)SOAP客戶端內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
附件:
使用項目文件(包含服務(wù)端程序) 提取碼: 4qpm
cpp-httplib-master-20210826備份 提取碼: n4yg
tinyxml2-master-20210921備份 提取碼: yjv8
相關(guān)文章
C語言詳解strcmp函數(shù)的分析及實現(xiàn)
strcmp函數(shù)語法為“int strcmp(char *str1,char *str2)”,其作用是比較字符串str1和str2是否相同,如果相同則返回0,如果不同,前者大于后者則返回1,否則返回-12022-05-05C++實現(xiàn)LeetCode(103.二叉樹的之字形層序遍歷)
這篇文章主要介紹了C++實現(xiàn)LeetCode(103.二叉樹的之字形層序遍歷),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07正確理解C++的構(gòu)造函數(shù)和析構(gòu)函數(shù)
在C++的學(xué)習(xí)中,可以把類當(dāng)作一個模具,類實例化出來的對象就是根據(jù)這個模具所產(chǎn)生的實體,對象看作是自己創(chuàng)建的一個新的數(shù)據(jù)類型。本文主要介紹了類對象通過拷貝函數(shù)進行初始化,分析類對象的內(nèi)存模型,以及通過this指針實現(xiàn)更復(fù)雜的功能。最后介紹了析構(gòu)函數(shù)的基礎(chǔ)知識2021-06-06