Qt 之 QtSoap(访问WebService)

原文地址::https://blog.csdn.net/liang19890820/article/details/51673800

相关文章

1、Qt之QtSoap(访问WebService)----https://www.cnblogs.com/itrena/p/5938317.html

2、Qt调用WebService----https://www.cnblogs.com/findumars/p/5290140.html

3、QT环境下Webservice服务调用,服务器端是在Java环境下----https://blog.csdn.net/weixin_40288738/article/details/88994875?utm_medium=distribute.pc_relevant_download.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-baidujs-1.nonecase

4、QT QHttp 访问 WEBSERVICE 范例----https://blog.csdn.net/cangwu_lee/article/details/1637722?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.baidujs&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.baidujs

5、QtSoap开发web services客户端程序----https://www.cnblogs.com/findumars/p/5040435.html

6、学习QtSoap----https://blog.csdn.net/qq_41622002/article/details/108848775

7、QtSoap调用Web Service(QtSoap是非官方应用)----https://www.cnblogs.com/findumars/p/5176087.html

8、QtSoap调用Web Service----https://www.xuebuyuan.com/2174829.html

9、Qt之QtSoap(访问WebService)----http://t.zoukankan.com/itrena-p-5938317.html

10、QtSoap传递多种类型参数----https://blog.csdn.net/zbc415766331/article/details/48313009?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.control&dist_request_id=1b945b0b-14dd-41f5-bdb0-fddc631be061&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.control

 

简述

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

版权声明:一去、二三里,未经博主允许不得转载。

下载编译

下载源码后,需要修改一些地方(我的版本是Qt5),按照错误提示修改即可,很简单。

编译完成之后会生成QtSolutions_SOAP-headd.lib、QtSolutions_SOAP-headd.dll。。。

使用

拷贝QtSolutions_SOAP-headd.lib至lib目录下,拷贝qtsoap.h至include目录下。

pro中添加库文件及库目录:

<span style="color:#000000"><code class="language-Qt">INCLUDEPATH <span style="color:#4f4f4f !important">+=</span> $<span style="color:#4f4f4f !important">$PWD</span>/include
LIBS <span style="color:#4f4f4f !important">+=</span> <span style="color:#98c379">-L</span>$<span style="color:#4f4f4f !important">$PWD</span>/lib <span style="color:#98c379">-lQtSolutions_SOAP</span><span style="color:#98c379">-headd</span></code></span>

实例解析

下面,我们以“获得腾讯QQ在线状态”为例,见:WebXml.com.cn,里面包含了大量的Web服务,例如:手机号码归属地查询,电子邮件地址验证、城市天气预报查询等。

示例

SOAP 1.1

以下是SOAP1.1请求和响应示例。所显示的占位符需替换为实际值。

这里写图片描述

效果

这里写图片描述

源码

<span style="color:#000000"><code class="language-Qt"><span style="color:#880000 !important"><em>// 构建控件</em></span>
m_pQQLabel <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QLabel(this);
m_pStateLabel <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QLabel(this);
m_pQQLineEdit <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QLineEdit(this);
m_pStateLineEdit <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QLineEdit(this);
m_pSubmitButton <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QPushButton(this);

m_pStateLineEdit<span style="color:#4f4f4f !important">-></span>setReadOnly(<span style="color:#006666 !important">true</span>);
m_pQQLabel<span style="color:#4f4f4f !important">-></span>setText(QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"QQ号码:"</span>));
m_pStateLabel<span style="color:#4f4f4f !important">-></span>setText(QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"QQ状态:"</span>));
m_pSubmitButton<span style="color:#4f4f4f !important">-></span>setText(QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"提交"</span>));

QGridLayout <span style="color:#4f4f4f !important">*</span>pLayout <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QGridLayout();
pLayout<span style="color:#4f4f4f !important">-></span>addWidget(m_pQQLabel, <span style="color:#006666 !important">0</span>, <span style="color:#006666 !important">0</span>);
pLayout<span style="color:#4f4f4f !important">-></span>addWidget(m_pQQLineEdit, <span style="color:#006666 !important">0</span>, <span style="color:#006666 !important">1</span>);
pLayout<span style="color:#4f4f4f !important">-></span>addWidget(m_pStateLabel, <span style="color:#006666 !important">1</span>, <span style="color:#006666 !important">0</span>);
pLayout<span style="color:#4f4f4f !important">-></span>addWidget(m_pStateLineEdit, <span style="color:#006666 !important">1</span>, <span style="color:#006666 !important">1</span>);
pLayout<span style="color:#4f4f4f !important">-></span>addWidget(m_pSubmitButton, <span style="color:#006666 !important">2</span>, <span style="color:#006666 !important">1</span>, <span style="color:#006666 !important">1</span>, <span style="color:#006666 !important">1</span>, Qt<span style="color:#006666 !important">::AlignRight</span>);
pLayout<span style="color:#4f4f4f !important">-></span>setSpacing(<span style="color:#006666 !important">10</span>);
pLayout<span style="color:#4f4f4f !important">-></span>setContentsMargins(<span style="color:#006666 !important">10</span>, <span style="color:#006666 !important">10</span>, <span style="color:#006666 !important">10</span>, <span style="color:#006666 !important">10</span>);
setLayout(pLayout);

<span style="color:#880000 !important"><em>// 连接信号槽</em></span>
m_pHttp <span style="color:#4f4f4f !important">=</span> <span style="color:#006666 !important">new</span> QtSoapHttpTransport(this);
connect(m_pSubmitButton, SIGNAL(clicked()), this, SLOT(onSubmit()));
connect(m_pHttp, SIGNAL(responseReady(const QtSoapMessage <span style="color:#4f4f4f !important">&</span>)), this, SLOT(onResponse(const QtSoapMessage <span style="color:#4f4f4f !important">&</span>)));

<span style="color:#880000 !important"><em>// 提交请求</em></span>
<span style="color:#006666 !important">void</span> MainWindow<span style="color:#006666 !important">::onSubmit</span>()
{
    QtSoapMessage message;

    <span style="color:#880000 !important"><em>// 设置方法</em></span>
    message<span style="color:#4f4f4f !important">.</span>setMethod(<span style="color:#009900 !important">"qqCheckOnline"</span>, <span style="color:#009900 !important">"http://WebXml.com.cn/"</span>);

    <span style="color:#880000 !important"><em>// 设置动作</em></span>
    m_pHttp<span style="color:#4f4f4f !important">-></span>setAction(<span style="color:#009900 !important">"http://WebXml.com.cn/qqCheckOnline"</span>);

    <span style="color:#880000 !important"><em>// 设置主机</em></span>
    m_pHttp<span style="color:#4f4f4f !important">-></span>setHost(<span style="color:#009900 !important">"www.webxml.com.cn"</span>);

    <span style="color:#880000 !important"><em>// 添加方法参数</em></span>
    QString strQQ <span style="color:#4f4f4f !important">=</span> m_pQQLineEdit<span style="color:#4f4f4f !important">-></span>text();
    message<span style="color:#4f4f4f !important">.</span>addMethodArgument(<span style="color:#009900 !important">"qqCode"</span>, <span style="color:#009900 !important">""</span>, strQQ);
    QString strXML <span style="color:#4f4f4f !important">=</span> message<span style="color:#4f4f4f !important">.</span>toXmlString();

    <span style="color:#880000 !important"><em>// 提交请求</em></span>
    m_pHttp<span style="color:#4f4f4f !important">-></span>submitRequest(message, <span style="color:#009900 !important">"/webservices/qqOnlineWebService.asmx"</span>);
}

<span style="color:#006666 !important">void</span> MainWindow<span style="color:#006666 !important">::onResponse</span>(const QtSoapMessage <span style="color:#4f4f4f !important">&</span>response)
{
    QString strXML <span style="color:#4f4f4f !important">=</span> response<span style="color:#4f4f4f !important">.</span>toXmlString();
    QDomDocument doc;
    doc<span style="color:#4f4f4f !important">.</span>setContent(strXML);

    <span style="color:#880000 !important"><em>// 接在在线状态</em></span>
    QDomNodeList nodeList <span style="color:#4f4f4f !important">=</span> doc<span style="color:#4f4f4f !important">.</span>elementsByTagName(<span style="color:#009900 !important">"qqCheckOnlineResult"</span>);
    <span style="color:#000088 !important">if</span> (<span style="color:#4f4f4f !important">!</span>nodeList<span style="color:#4f4f4f !important">.</span>isEmpty())
    {
        QDomNode node <span style="color:#4f4f4f !important">=</span> nodeList<span style="color:#4f4f4f !important">.</span>at(<span style="color:#006666 !important">0</span>);
        QString strResult <span style="color:#4f4f4f !important">=</span> node<span style="color:#4f4f4f !important">.</span>toElement()<span style="color:#4f4f4f !important">.</span>text();
        QString strState(<span style="color:#009900 !important">"N/A"</span>);
        <span style="color:#000088 !important">if</span> (QString<span style="color:#006666 !important">::compare</span>(strResult, <span style="color:#009900 !important">"Y"</span>) <span style="color:#4f4f4f !important">==</span><span style="color:#006666 !important">0</span>)
        {
            strState <span style="color:#4f4f4f !important">=</span> QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"在线"</span>);
        }
        <span style="color:#000088 !important">else</span> <span style="color:#000088 !important">if</span> (QString<span style="color:#006666 !important">::compare</span>(strResult, <span style="color:#009900 !important">"N"</span>) <span style="color:#4f4f4f !important">==</span> <span style="color:#006666 !important">0</span>)
        {
            strState <span style="color:#4f4f4f !important">=</span> QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"离线"</span>);
        }
        <span style="color:#000088 !important">else</span> <span style="color:#000088 !important">if</span> (QString<span style="color:#006666 !important">::compare</span>(strResult, <span style="color:#009900 !important">"E"</span>) <span style="color:#4f4f4f !important">==</span> <span style="color:#006666 !important">0</span>)
        {
            strState <span style="color:#4f4f4f !important">=</span> QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"QQ号码错误"</span>);
        }
        <span style="color:#000088 !important">else</span> <span style="color:#000088 !important">if</span> (QString<span style="color:#006666 !important">::compare</span>(strResult, <span style="color:#009900 !important">"A"</span>) <span style="color:#4f4f4f !important">==</span> <span style="color:#006666 !important">0</span>)
        {
            strState <span style="color:#4f4f4f !important">=</span> QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"商业用户验证失败"</span>);
        }
        <span style="color:#000088 !important">else</span> <span style="color:#000088 !important">if</span> (QString<span style="color:#006666 !important">::compare</span>(strResult, <span style="color:#009900 !important">"V"</span>) <span style="color:#4f4f4f !important">==</span> <span style="color:#006666 !important">0</span>)
        {
            strState <span style="color:#4f4f4f !important">=</span> QString<span style="color:#006666 !important">::fromLocal8Bit</span>(<span style="color:#009900 !important">"免费用户超过数量"</span>);
        }
        m_pStateLineEdit<span style="color:#4f4f4f !important">-></span>setText(strState);
    }
}</code></span>
 

我们也可以使用qq号码进行在线验证:qqCheckOnline

更多参考

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值