QtSoap 访问webService

本文介绍了如何在Qt5.12环境中利用QtSoap库访问WebService,包括从官网下载源码、解决编译时错误、以及新增传递不同类型参数的方法,如Double和字符串数组。作者还分享了自定义SOAP请求XML的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明

参考链接:Qt 之 QtSoap(访问WebService)-优快云博客

开发环境:QT 5.12 + VS2017  windows X64  MSVC

Qt本身给我们提供了调用WebService的解决方案QtSoap,源码及示例见:qt-solutions-qtsoap

 编译

1、从官网下载源码

2、使用QTCreator打开buildlib.pro,如下图

3、选中buildlib,执行构建(遇到下图错误,把toAscii改成toUtf8)

qtsoap.cpp:3117:54: error: no member named 'toAscii' in 'QString

4、构建,qmake完成后在lib目录下就能看到动静态库

新增:传递多种类型参数

参考链接:QtSoap传递多种类型参数_qt soap-优快云博客

//头文件

QtSoapSimpleType(const QtSoapQName &name, Type type, QVariant variant);

//源文件

QtSoapSimpleType::QtSoapSimpleType(const QtSoapQName &name, Type type, QVariant variant): QtSoapType(name, type), v(variant){
}

//使用方法

QtSoapMesssage request;  
request.addMethodArgument(new QtSoapSimpleType(QtSoapQName("test"), QtSoapType::Double, QVariant(2015.9))); 

相关使用感想 

本来想上传字符串数组类型,使用原生Array类型弄了半天没弄好,最后直接通过submitRequest

上传原始xml数据,自己DIY

#if 0
	QString soapRequest = QString(
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
		"<SOAP-ENV:Envelope "
		"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
		"xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\" "
		"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
		"<SOAP-ENV:Body xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
		"<MyWebMethod2 xmlns=\"http://WebXml.com.cn/\">"
		"<param1 xsi:type=\"soapenc:Array\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" "
		"xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" soapenc:arrayType=\"xsd:string[]\">"
		"<string xsi:type=\"xsd:string\">aaaaaaaa</string>"
		"</param1>"
		"</MyWebMethod2>"
		"</SOAP-ENV:Body>"
		"</SOAP-ENV:Envelope>");
#endif

QString WebServiceModule::createSoapRequestXml(QStringList methodList, QVariantList paramList, QString stringListName, QStringList stringList)
{
	QDomDocument doc;
	// 创建SOAP Envelope元素
	QDomElement envelope = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "SOAP-ENV:Envelope");
	doc.appendChild(envelope);

	// 设置Envelope的属性
	//envelope.setAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
	envelope.setAttribute("xmlns:xsd", "http://www.w3.org/1999/XMLSchema");
	envelope.setAttribute("SOAP-ENV:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/");

	// 显式设置XML声明(这一步在Qt中通常是自动处理的,但这里为了明确展示而单独指出)
	QDomProcessingInstruction pi = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
	doc.insertBefore(pi, envelope);


	// 创建Body元素
	QDomElement body = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "SOAP-ENV:Body");
	envelope.appendChild(body);

	// 创建MyWebMethod2元素
	//QDomElement myWebMethod2 = doc.createElementNS("http://WebXml.com.cn/", "MyWebMethod2");
	QDomElement myWebMethod2 = doc.createElementNS(methodList[1], methodList[0]);
	body.appendChild(myWebMethod2);

	

	//判断是否存在字符串数组
	if (stringListName != "")
	{
		// 创建strList元素及其属性
		QDomElement strList = doc.createElement(stringListName);
		myWebMethod2.appendChild(strList);

		// 设置strList的属性
		QDomAttr typeAttr = doc.createAttributeNS("http://www.w3.org/1999/XMLSchema-instance", "xsi:type");
		typeAttr.setValue("soapenc:Array");
		strList.setAttributeNode(typeAttr);

		QDomAttr arrayTypeAttr = doc.createAttributeNS("http://www.w3.org/1999/XMLSchema-instance", "soapenc:arrayType");
		arrayTypeAttr.setValue("xsd:string[]");
		strList.setAttributeNode(arrayTypeAttr);

		for (int i = 0; i < stringList.size() && stringList.size() != 0; i++)
		{
			// 在strList下添加字符串元素
			QDomElement stringElement = doc.createElement("string");
			strList.appendChild(stringElement);

			// 设置string元素的属性
			QDomAttr stringTypeAttr = doc.createAttributeNS("http://www.w3.org/1999/XMLSchema-instance", "xsi:type");
			stringTypeAttr.setValue("xsd:string");
			stringElement.setAttributeNode(stringTypeAttr);

			stringElement.appendChild(doc.createTextNode(stringList[i]));
		}
	}

	//添加正常参数
	if (paramList.size() > 0 && paramList[0] != "")
	{
		for (int i = 0; i < paramList.size() && paramList.size() != 0; i += 2)
		{
			// 创建param元素及其属性
			QDomElement param = doc.createElement(paramList[i].toString());
			myWebMethod2.appendChild(param);

			// 设置param的属性
			if (paramList[i + 1].type() == QVariant::Int)
				param.setAttribute("xsi:type", "xsd:int");
			else if (paramList[i + 1].type() == QVariant::Bool)
				param.setAttribute("xsi:type", "xsd:bool");
			else
				param.setAttribute("xsi:type", "xsd:string");

			param.setAttribute("xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance");
			param.appendChild(doc.createTextNode(paramList[i + 1].toString()));
		}

	}
	// 输出完整的XML
	QString soapRequest = doc.toString();
	return soapRequest;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值