QtSoap of Qt (Access WebService)

Original address: https://blog.csdn.net/liang19890820/article/details/51673800

related articles

1. QtSoap of Qt (Access WebService) ---- https://www.cnblogs.com/itrena/p/5938317.html

2. Qt calls WebService ---- https://www.cnblogs.com/findumars/p/5290140.html

3. The Webservice service is called in the QT environment, and the server side is in the Java environment ---- 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 develop web services client ---- https://www.cnblogs.com/findumars/p/5040435.html

6. Learn QtSoap---- https://blog.csdn.net/qq_41622002/article/details/108848775

7. QtSoap calls Web Service (QtSoap is an unofficial application) ---- https://www.cnblogs.com/findumars/p/5176087.html

8. QtSoap calls Web Service---- https://www.xuebuyuan.com/2174829.html

9. QtSoap of Qt (Access 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

 

Brief description

Qt itself provides us with QtSoap, a solution to call WebService. See the source code and examples: qt-solutions-qtsoap

Copyright statement: Yiqu, Ersanli, can not be reprinted without the permission of the blogger.

Download and compile

After downloading the source code, you need to modify some places (my version is Qt5), just follow the error prompt to modify it, it's very simple.

After compilation, QtSolutions_SOAP-headd.lib and QtSolutions_SOAP-headd.dll will be generated. . .

use

Copy QtSolutions_SOAP-headd.lib to the lib directory, and copy qtsoap.h to the include directory.

Add library files and library directories in 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>

Example analysis

Below, we take "Get Tencent QQ online status" as an example, see: WebXml.com.cn , which contains a large number of Web services, such as: mobile phone number attribution query, email address verification, city weather forecast query, etc.

Example

SOAP 1.1

The following is an example of SOAP 1.1 request and response. The displayed placeholders need to be replaced with actual values.

Write picture description here

effect

Write picture description here

Source code

<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>
 

We can also use the qq number for online verification: qqCheckOnline

More references

Guess you like

Origin blog.csdn.net/xqhrs232/article/details/113786983