landuochong 发表于 2011-12-21 08:44

SOAP_and_XML_Web_services_for_Qt_apps

<h2>转自:http://wiki.forum.nokia.com/index.php/GSoap:_SOAP_and_XML_Web_services_for_Qt_apps<br><span class="mw-headline" id="Introduction"></span></h2><h2><span class="mw-headline" id="Introduction">Introduction</span></h2>
<p>The gSOAP toolkit is an open source C and C++ software development
toolkit for SOAP/XML Web services and generic (non-SOAP) C/C++ XML data
bindings. The toolkit analyzes WSDLs and XML schemes (separately or as a
combined set) and maps the XML scheme types and the SOAP messaging
protocols to easy-to-use and efficient C and C++ code.
The toolkit is mature, supports Symbian as well as Maemo/MeeGo; it
supports many industry-standard protocols (SOAP 1.1/1.2, WSDL 1.1...)
and transports (HTTP/S, TCP, UDP (SOAP-over-UDP), MIME (SwA), DIME-
streaming, MTOM - streaming, HTTP1.0/1.1, IPv4, IPv6, RSS, XML-RPC,
WS-Addressing).
</p>
<h2><span class="editsection">[<a href="http://wiki.forum.nokia.com/index.php?title=GSoap:_SOAP_and_XML_Web_services_for_Qt_apps&amp;action=edit&amp;section=2" title="Edit section: Goal">edit</a>]</span> <span class="mw-headline" id="Goal"> Goal </span></h2>
<p>Goal of this article is to show how to create an application which
makes use of web services using gSOAP.This app will be able to get the
NOK (Nokia) stock quote.
</p>
<h2><span class="editsection">[<a href="http://wiki.forum.nokia.com/index.php?title=GSoap:_SOAP_and_XML_Web_services_for_Qt_apps&amp;action=edit&amp;section=3" title="Edit section: Steps">edit</a>]</span> <span class="mw-headline" id="Steps">Steps</span></h2>
<ul><li> First of all, you need to download the scheme of the web service to use: in this example the scheme has been downloaded from <a href="http://www.webservicex.net/stockquote.asmx?WSDL" class="external text" rel="nofollow">webservicex.net</a>
</li><li> The WSDL scheme has been saved in the file stockquote.wsdl
</li><li> The wsdl2h tool has been used to generate a .h file from the WSDL one: <i>wsdl2h stockquote.wsdl</i>
</li><li> The header file is then processed by the soapcpp2 tool to generate proxies and service objects: <i>soapcpp2 -I/usr/include/gsoap/ stockquote.h</i>
</li></ul>
<pre class="text" style="font-family:monospace;">gnuton@joshua:/tmp/SOAP2$ soapcpp2 -I/usr/include/gsoap/ stockquote.h<br>&nbsp;<br>**The gSOAP Stub and Skeleton Compiler for C and C++ 2.7.9l<br>**Copyright (C) 2000-2007, Robert van Engelen, Genivia Inc.<br>**All Rights Reserved. This product is provided "as is", without any warranty.<br>**The gSOAP compiler is released under one of the following three licenses:<br>**GPL, the gSOAP public license, or the commercial license by Genivia Inc.<br>&nbsp;<br>Saving soapStub.h<br>Saving soapH.h<br>Saving soapC.cpp<br>Saving soapClient.cpp<br>Saving soapClientLib.cpp<br>Saving soapServer.cpp<br>Saving soapServerLib.cpp<br>Using ns2 service name: StockQuoteSoap<br>Using ns2 service style: document<br>Using ns2 service encoding: literal<br>Using ns2 service location: http://www.webservicex.net/stockquote.asmx<br>Using ns2 schema namespace: http://www.webserviceX.NET/StockQuoteSoap<br>Saving soapStockQuoteSoapProxy.h client proxy<br>Saving soapStockQuoteSoapObject.h server object<br>Saving StockQuoteSoap.GetQuote.req.xml sample SOAP/XML request<br>Saving StockQuoteSoap.GetQuote.res.xml sample SOAP/XML response<br>Saving StockQuoteSoap.nsmap namespace mapping table<br>Using ns3 service name: StockQuoteSoap12<br>Using ns3 service style: document<br>Using ns3 service encoding: literal<br>Using ns3 service location: http://www.webservicex.net/stockquote.asmx<br>Using ns3 schema namespace: http://www.webserviceX.NET/StockQuoteSoap12<br>Saving soapStockQuoteSoap12Proxy.h client proxy<br>Saving soapStockQuoteSoap12Object.h server object<br>Saving StockQuoteSoap12.GetQuote.req.xml sample SOAP/XML request<br>Saving StockQuoteSoap12.GetQuote.res.xml sample SOAP/XML response<br>Saving StockQuoteSoap12.nsmap namespace mapping table<br>&nbsp;<br>Compilation successful</pre>
<ul><li> From this step, the Nokia Qt SDK can be used to develop our client
</li><li> To keep the example minimal and clean, we used the Qt console template to create this example.
</li><li> Since gSoap tools generate many files, they have been put in
the soap/ dir, avoiding mixing them with the Qt files written by us;
</li></ul>
<pre class="text" style="font-family:monospace;">gnuton@joshua:~/ARTICOLI-WIKI/2Q-2010/7.GSOAP/soap2$ ls gsoap/<br>soapC.cpp          soapServer.cpp                soapStockQuoteSoapObject.hStockQuoteSoap12.GetQuote.req.xmlStockQuoteSoap.GetQuote.res.xml<br>soapClient.cpp   soapServerLib.cpp             soapStockQuoteSoapProxy.h   StockQuoteSoap12.GetQuote.res.xmlStockQuoteSoap.nsmap<br>soapClientLib.cppsoapStockQuoteSoap12Object.hsoapStub.h                  StockQuoteSoap12.nsmap             stockquote.wsdl<br>soapH.h            soapStockQuoteSoap12Proxy.h   stockquote.h                StockQuoteSoap.GetQuote.req.xml</pre>
<ul><li> The most interesting file among those generated is the proxy
class in the file gsoap/soapStockQuoteSoapProxy.h which looks like this:
</li></ul>
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> StockQuoteSoap<br><span style="color: #008000;">{</span>   <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br>      <span style="color: #666666;">/// Constructor allocates soap engine context, sets default endpoint URL, and sets namespace mapping table</span><br>      StockQuoteSoap<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span><br>&nbsp;<br>      <span style="color: #666666;">/// Destructor frees deserialized data and soap engine context</span><br>      <span style="color: #0000ff;">virtual</span> ~StockQuoteSoap<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span><br>&nbsp;<br>      <span style="color: #666666;">/// Invoke 'GetQuote' of service 'StockQuoteSoap' and return error code (or SOAP_OK)</span><br>      <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">int</span> __ns2__GetQuote<span style="color: #008000;">(</span>_ns1__GetQuote <span style="color: #000040;">*</span>ns1__GetQuote, _ns1__GetQuoteResponse <span style="color: #000040;">*</span>ns1__GetQuoteResponse<span style="color: #008000;">)</span><span style="color: #008080;">;</span><br><span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre>
<ul><li> you can now modify the main.cpp file as follows:
</li></ul>
<pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;QtCore/QCoreApplication&gt;</span><br><span style="color: #339900;">#include "gsoap/soapStockQuoteSoapProxy.h"</span><br><span style="color: #339900;">#include "gsoap/StockQuoteSoap.nsmap"</span><br>&nbsp;<br><span style="color: #339900;">#include &lt;QDebug&gt;</span><br>&nbsp;<br><span style="color: #0000ff;">int</span> main<span style="color: #008000;">(</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">[</span><span style="color: #008000;">]</span><span style="color: #008000;">)</span><br><span style="color: #008000;">{</span><br>    QCoreApplication a<span style="color: #008000;">(</span>argc, argv<span style="color: #008000;">)</span><span style="color: #008080;">;</span><br>&nbsp;<br>    StockQuoteSoap soapObj<span style="color: #008080;">;</span><br>&nbsp;<br>    _ns1__GetQuote <span style="color: #000040;">*</span>quote <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> _ns1__GetQuote<span style="color: #008080;">;</span><br>    _ns1__GetQuoteResponse <span style="color: #000040;">*</span>response <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> _ns1__GetQuoteResponse<span style="color: #008080;">;</span><br>&nbsp;<br>    std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> s <span style="color: #000080;">=</span> <span style="color: #FF0000;">"NOK"</span><span style="color: #008080;">;</span><br>    quote<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>symbol <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>s<span style="color: #008080;">;</span><br>    soapObj.__ns2__GetQuote<span style="color: #008000;">(</span>quote, response<span style="color: #008000;">)</span><span style="color: #008080;">;</span><br>    qDebug<span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">"RESP"</span> <span style="color: #000080;">&lt;&lt;</span> response<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetQuoteResult<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>c_str<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span><br>    <span style="color: #0000ff;">return</span> a.<span style="color: #007788;">exec</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span><br><span style="color: #008000;">}</span></pre>
<ul><li> And to make the compilation working you have to add the required files to the Qt project file:
</li></ul>
<pre class="text" style="font-family:monospace;">QT       += core<br>QT       -= gui<br>&nbsp;<br>TARGET = soap<br>CONFIG   += console<br>CONFIG   -= app_bundle<br>&nbsp;<br>CONFIG += link_pkgconfig<br>PKGCONFIG += gsoap++<br>&nbsp;<br>TEMPLATE = app<br>&nbsp;<br>&nbsp;<br>SOURCES += main.cpp \<br>    gsoap/soapC.cpp \<br>    gsoap/soapClient.cpp<br>&nbsp;<br>HEADERS += \<br>    gsoap/soapStockQuoteSoapProxy.h \<br>    gsoap/soapH.h \<br>    gsoap/soapStub.h \<br>    gsoap/soapStockQuoteSoapObject.h<br>&nbsp;<br>OTHER_FILES += \<br>    gsoap/StockQuoteSoap.nsmap</pre>
<ul><li> AND NOW THE EXAMPLE IS READY TO BE BUILT. If the web service is up, the application will print out the Nokia stock quote.
</li></ul>
<h2><span class="editsection">[<a href="http://wiki.forum.nokia.com/index.php?title=GSoap:_SOAP_and_XML_Web_services_for_Qt_apps&amp;action=edit&amp;section=4" title="Edit section: Code">edit</a>]</span> <span class="mw-headline" id="Code"> Code </span></h2>
<p>Click <a href="http://www.forum.nokia.com/piazza/wiki/images/9/9f/Soap.zip?20101223132759" class="internal" title="Soap.zip">Media:soap.zip</a> to download the example.
</p>
               
               
               
               
页: [1]
查看完整版本: SOAP_and_XML_Web_services_for_Qt_apps