Qt之QtSoap(访问WebService)
简述
Qt本身给我们提供了调用WebService的解决方案QtSoap,源码及示例见:qt-solutions-qtsoap
下载编译
下载源码后,需要修改一些地方(我的版本是Qt5),按照错误提示修改即可,很简单。
编译完成之后会生成QtSolutions_SOAP-headd.lib、QtSolutions_SOAP-headd.dll。。。
使用
拷贝QtSolutions_SOAP-headd.lib至lib目录下,拷贝qtsoap.h至include目录下。
pro中添加库文件及库目录:
INCLUDEPATH += $$PWD/include
LIBS += -L$$PWD/lib -lQtSolutions_SOAP-headd
实例解析
下面,我们以“获得腾讯QQ在线状态”为例,见:WebXml.com.cn,里面包含了大量的Web服务,例如:手机号码归属地查询,电子邮件地址验证、城市天气预报查询等。
示例
SOAP 1.1
以下是SOAP1.1请求和响应示例。所显示的占位符需替换为实际值。
效果
源码
// 构建控件
m_pQQLabel = new QLabel(this);
m_pStateLabel = new QLabel(this);
m_pQQLineEdit = new QLineEdit(this);
m_pStateLineEdit = new QLineEdit(this);
m_pSubmitButton = new QPushButton(this);
m_pStateLineEdit->setReadOnly(true);
m_pQQLabel->setText(QString::fromLocal8Bit("QQ号码:"));
m_pStateLabel->setText(QString::fromLocal8Bit("QQ状态:"));
m_pSubmitButton->setText(QString::fromLocal8Bit("提交"));
QGridLayout *pLayout = new QGridLayout();
pLayout->addWidget(m_pQQLabel, 0, 0);
pLayout->addWidget(m_pQQLineEdit, 0, 1);
pLayout->addWidget(m_pStateLabel, 1, 0);
pLayout->addWidget(m_pStateLineEdit, 1, 1);
pLayout->addWidget(m_pSubmitButton, 2, 1, 1, 1, Qt::AlignRight);
pLayout->setSpacing(10);
pLayout->setContentsMargins(10, 10, 10, 10);
setLayout(pLayout);
// 连接信号槽
m_pHttp = new QtSoapHttpTransport(this);
connect(m_pSubmitButton, SIGNAL(clicked()), this, SLOT(onSubmit()));
connect(m_pHttp, SIGNAL(responseReady(const QtSoapMessage &)), this, SLOT(onResponse(const QtSoapMessage &)));
// 提交请求
void MainWindow::onSubmit()
{
QtSoapMessage message;
// 设置方法
message.setMethod("qqCheckOnline", "http://WebXml.com.cn/");
// 设置动作
m_pHttp->setAction("http://WebXml.com.cn/qqCheckOnline");
// 设置主机
m_pHttp->setHost("www.webxml.com.cn");
// 添加方法参数
QString strQQ = m_pQQLineEdit->text();
message.addMethodArgument("qqCode", "", strQQ);
QString strXML = message.toXmlString();
// 提交请求
m_pHttp->submitRequest(message, "/webservices/qqOnlineWebService.asmx");
}
void MainWindow::onResponse(const QtSoapMessage &response)
{
QString strXML = response.toXmlString();
QDomDocument doc;
doc.setContent(strXML);
// 接在在线状态
QDomNodeList nodeList = doc.elementsByTagName("qqCheckOnlineResult");
if (!nodeList.isEmpty())
{
QDomNode node = nodeList.at(0);
QString strResult = node.toElement().text();
QString strState("N/A");
if (QString::compare(strResult, "Y") ==0)
{
strState = QString::fromLocal8Bit("在线");
}
else if (QString::compare(strResult, "N") == 0)
{
strState = QString::fromLocal8Bit("离线");
}
else if (QString::compare(strResult, "E") == 0)
{
strState = QString::fromLocal8Bit("QQ号码错误");
}
else if (QString::compare(strResult, "A") == 0)
{
strState = QString::fromLocal8Bit("商业用户验证失败");
}
else if (QString::compare(strResult, "V") == 0)
{
strState = QString::fromLocal8Bit("免费用户超过数量");
}
m_pStateLineEdit->setText(strState);
}
}
我们也可以使用qq号码进行在线验证:qqCheckOnline