首先需要下载QtSoap开源包,下载地址为:
http://www.filestube.com/q/qtsoap+download,
我使用的是:qtsoap-2.6-opensource(不需要安装,直接解压到某个目录即可)。
如果你从未使用过QtSoap,那么先学习其中的Demo,在目录"examples"中,有easter,google和population 三个例子。
Note to Qt Visual Studio Integration users: In the instructions below, instead of building from command line with nmake, you can use the menu command 'Qt->Open Solution from .pro file' on the .pro files in the example and plugin directories, and then build from within Visual Studio.
如果是使用的的是VS+Qt开发方式,在使用菜单中的"Qt→Open Qt Project File (.pro)... 来打开以上工程文件。
本人使用的是VS2010,在采用以上方法时,仍然无法正常载入,于是我先用VS2005打开,在VS2005里对以上项目进行保存,再在VS2010里重新打开,当然在VS2010里重新打开时会提示需要进行格式转换,成功后你就可以编译运行Demo了。
本人先重写了:population,主要是为了熟悉下QtSoap以及开发配置。
1 在头文件(xxx.h)定义变量(XXX为类名,请您重新定义)。
#include
<qtsoap.h>
class QTextEdit;
private slots:
void
getResponse();
private:
QTextEdit
*m_pWeatherEdt;
private:
QtSoapHttpTransport http;
2 在类的构造函数中:
m_pWeatherEdt =
new QTextEdit(
this);
connect(&http, SIGNAL(responseReady()), SLOT(getResponse()));
3 请求发送:
void XXX::
submitRequest()
{
http.
setHost(" www.abundanttech.com");
QtSoapMessage
request;
http.
setAction (" http://www.abundanttech.com/WebServices/Population/getPopulation");
request.
setMethod("getPopulation", " http://www.abundanttech.com/WebServices/Population");
request.
addMethodArgument
("strCountry",
"",
"china");
http.
submitRequest(request,
"/WebServices/Population/population.asmx");
}
以上参数请查看: http://www.abundanttech.com/WebServices/Population/population.asmx
如下:
SOAP
The following is a sample SOAP request and response. The placeholders shown need to be replaced with actual values.
POST /WebServices/Population/population.asmx HTTP/1.1
Host: www.abundanttech.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.abundanttech.com/WebServices/Population/getPopulation"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getPopulation xmlns="http://www.abundanttech.com/WebServices/Population">
<strCountry>string</strCountry>
</getPopulation>
</soap:Body>
</soap:Envelope>
当然,你也可以使用“中国气象局”的web services来做测试示例
void XXX::submitRequest()
{
http.setHost("www.ayandy.com");
QtSoapMessage request;
http.setAction("http://tempuri.org/getSupportProvince
");
request.setMethod("getSupportProvince", "http://tempuri.org/
");
http.submitRequest(request, "/Service.asmx");
}
以上参数请查看: http://www.ayandy.com/Service.asmx如下:
SOAP 1.1
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
POST /Service.asmx HTTP/1.1
Host: www.ayandy.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/getSupportProvince"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getSupportProvince xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>
4 回复解析:
void XXX::getResponse()
{
// Get a reference to the response message.
const QtSoapMessage &message = http.getResponse();
// Check if the response is a SOAP Fault message
if (message.isFault())
{
m_pWeatherEdt->append(message.faultString().value().toString().toLatin1().constData());
}
else
{
// Get the return value, and print the result.
const QtSoapType &response = message.returnValue();
m_pWeatherEdt->append(response["Country"].value().toString().toLatin1().constData());
m_pWeatherEdt->append(response["Pop"].value().toString().toLatin1().constData());
m_pWeatherEdt->append(response["Date"].value().toString().toLatin1().constData());
}
}
以上方法只能解析固定类型的消息,下面介绍一种通用方法:
void XXX::getResponse()
{
// Get a reference to the response message.
const QtSoapMessage &message = http.getResponse();
// Check if the response is a SOAP Fault message
if (message.isFault())
{
m_pWeatherEdt->append(message.faultString().value().toString().toLatin1().constData());
}
else
{
const QtSoapType &root = message.returnValue();
QtSoapStruct myStruct((QtSoapStruct&)root);
//将返回的结构体转换成
QtSoapStruct
for (int i = 0; i < myStruct.count(); i++)
{
m_pWeatherEdt->append(myStruct[i].typeName() + " : " + myStruct[i].toString());
}
}
}