大佬教程收集整理的这篇文章主要介绍了C++实现一个SOAP客户端,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_944_1@
@H_944_1@
在C++中,一般使用gSOAP来实现客户端、服务端。然而,对小项目来说gSOAP太大了,也不太方便。我们完全可以自己实现SOAP协议,毕竟SOAP协议的本质就是:http协议+XML。@H_944_1@
文章C++中gSOAP的使用介绍了gSOAP的使用,本文就以它的服务端为例,实现一个SOAP客户端。这里需要使用下面两个库:@H_944_1@
库的使用方法可以参考以下文章或github:@H_944_1@
一个SAOP客户端的主要工作流程有3步:构建请求数据的xml、执行http协议的POST方法、解析响应数据的xml。@H_944_1@
准备请求数据、响应数据的xml文件,请求数据的xml文件在项目中作为模板使用,响应数据的xml文件仅用于开发参考不是项目必须的文件。 请求数据的xml内容如下:@H_944_1@
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://scheR_461_11845@as.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://scheR_461_11845@as.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLscheR_461_11845@a-instance"
xmlns:xsd="http://www.w3.org/2001/XMLscheR_461_11845@a"
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内容如下:@H_944_1@
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://scheR_461_11845@as.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://scheR_461_11845@as.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLscheR_461_11845@a-instance"
xmlns:xsd="http://www.w3.org/2001/XMLscheR_461_11845@a"
xmlns:ns="http://tempuri.org/ns.xsd">
<SOAP-ENV:Body>
<ns:addResponse>
<result>0.0</result>
</ns:addResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
头文件引用如下:@H_944_1@
#include "httplib.h"
#include"Tinyxml2.h"
#include <iostream>
#include <String>
using namespace std;
using namespace Tinyxml2;
项目文件如下:
使用Tinyxml的文档对象加载xml文件,设置文档对象的节点内容,然后返回xml内容,代码如下:@H_944_1@
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();
}
构建一个httplib的客户端对象,直接执行POST方法,代码如下:@H_944_1@
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方法。@H_944_1@
根据http方法返回的Result对象判断方法是否成功,Result对象有operator bool() const { return res_ != nullptr; }重载可以直接判断,代码如下:@H_944_1@
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的方法如下:@H_944_1@
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;
}
先启动服务端,在启动客户端的调试,结果如下:@H_944_1@
200
text/xml; charset=utf-8
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://scheR_461_11845@as.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://scheR_461_11845@as.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLscheR_461_11845@a-instance" xmlns:xsd="http://www.w3.org/2001/XMLscheR_461_11845@a" 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客户端所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。