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內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? 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內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? 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 引入庫文件
頭文件引用如下:
1
2
3
4
5
6
|
#include "httplib.h" #include"tinyxml2.h" #include < iostream > #include < string > using namespace std; using namespace tinyxml2; |
項目文件如下:
2.3 構建請求數據的xml
使用tinyxml
的文檔對象加載xml文件,設置文檔對象的節點內容,然后返回xml內容,
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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方法,
代碼如下:
1
2
3
4
5
|
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; }
重載可以直接判斷,
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
|
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的方法如下:
1
2
3
4
5
6
7
8
9
10
|
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、測試客戶端
先啟動服務端,在啟動客戶端的調試,結果如下:
1
2
3
4
5
6
|
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
原文鏈接:https://www.cnblogs.com/timefiles/p/HttplibTtinyxml2.html