製作一個calc的webservice

下載gsoap toolkit, linux必須安裝byacc, flex, bsion, openssl lib.

./configure --without-yacc (需要補上without-yacc,不然會出現-ly找不到的問題)

安裝完之後,主要會有兩個執行檔 

wsdl2h 解晰wsdl檔案,產生soapcpp2需要用到的header檔案

soapcpp2 利用wsdl2h產生的檔案,製作出client和server需要的程式碼

wsdl檔案範例:calc.wsdl

<?xml version="1.0" encoding="UTF-8"?>

<definitions name="calc"
 targetNamespace="http://192.168.26.1/calc.wsdl"
 xmlns:tns="http://192.168.26.1/calc.wsdl"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:ns="urn:calc"
 xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/"
 xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/"
 xmlns="http://schemas.xmlsoap.org/wsdl/">


<types>


 <schema targetNamespace="urn:calc"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ns="urn:calc"
  xmlns="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="unqualified"
  attributeFormDefault="unqualified">
  <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
 </schema>


</types>


<message name="addRequest">
 <part name="a" type="xsd:double"/>
 <part name="b" type="xsd:double"/>
</message>


<message name="addResponse">
 <part name="result" type="xsd:double"/>
</message>


<message name="subRequest">
 <part name="a" type="xsd:double"/>
 <part name="b" type="xsd:double"/>
</message>


<message name="subResponse">
 <part name="result" type="xsd:double"/>
</message>


<message name="mulRequest">
 <part name="a" type="xsd:double"/>
 <part name="b" type="xsd:double"/>
</message>


<message name="mulResponse">
 <part name="result" type="xsd:double"/>
</message>


<message name="divRequest">
 <part name="a" type="xsd:double"/>
 <part name="b" type="xsd:double"/>
</message>


<message name="divResponse">
 <part name="result" type="xsd:double"/>
</message>


<message name="powRequest">
 <part name="a" type="xsd:double"/>
 <part name="b" type="xsd:double"/>
</message>


<message name="powResponse">
 <part name="result" type="xsd:double"/>
</message>


<portType name="calcPortType">
 <operation name="add">
  <documentation>Service definition of function ns__add</documentation>
  <input message="tns:addRequest"/>
  <output message="tns:addResponse"/>
 </operation>
 <operation name="sub">
  <documentation>Service definition of function ns__sub</documentation>
  <input message="tns:subRequest"/>
  <output message="tns:subResponse"/>
 </operation>
 <operation name="mul">
  <documentation>Service definition of function ns__mul</documentation>
  <input message="tns:mulRequest"/>
  <output message="tns:mulResponse"/>
 </operation>
 <operation name="div">
  <documentation>Service definition of function ns__div</documentation>
  <input message="tns:divRequest"/>
  <output message="tns:divResponse"/>
 </operation>
 <operation name="pow">
  <documentation>Service definition of function ns__pow</documentation>
  <input message="tns:powRequest"/>
  <output message="tns:powResponse"/>
 </operation>
</portType>


<binding name="calc" type="tns:calcPortType">
 <SOAP:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
 <operation name="add">
  <SOAP:operation style="rpc" soapAction=""/>
  <input>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </input>
  <output>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </output>
 </operation>
 <operation name="sub">
  <SOAP:operation style="rpc" soapAction=""/>
  <input>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </input>
  <output>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </output>
 </operation>
 <operation name="mul">
  <SOAP:operation style="rpc" soapAction=""/>
  <input>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </input>
  <output>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </output>
 </operation>
 <operation name="div">
  <SOAP:operation style="rpc" soapAction=""/>
  <input>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </input>
  <output>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </output>
 </operation>
 <operation name="pow">
  <SOAP:operation style="rpc" soapAction=""/>
  <input>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </input>
  <output>
     <SOAP:body use="encoded" namespace="urn:calc" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </output>
 </operation>
</binding>


<service name="calc">
 <documentation>gSOAP 2.7.9k generated service definition</documentation>
 <port name="calc" binding="tns:calc">
  <SOAP:address location="http://192.168.26.1:4000/cgi-bin/calcserver.cgi"/>
 </port>
</service>


</definitions>

 

執行範例:


REM -c  generate C source code
REM -g generate global top-level element declarations
REM -s don't generate STL code (no std::string and no std::vector)
REM -v  verbose output  
REM -y generate typedef synonyms for structs and enums
REM -o file output to file

wsdl2h -vsgyc -o calc.h calc.wsdl

REM -2 Use SOAP 1.2 namespaces and encodings
REM -c Generate pure C code
REM -L Do not generate soapClientLib/soapServerLib
REM -T Generate server auto-test code
REM -d < path > Save sources in directory specified by < path >
REM -I < path > Use < path > (or paths separated with `:') for #import

soapcpp2 -I gsoap-2.8\import -d out -2cLT calc.h

 

編譯web service(server)的方式,soapTester.c是自動產生的空殼,所以透過實作soapTester.c裡面的functions達到設計的功能

gcc soapTester.c soapServer.c soapC.c -o calcserver.cgi -lgsoap -lm

編譯web service(client)的方式

gcc calc_cli.c soapC.c soapClient.c -o calc_cli -lgsoap

其中calc_cli.c的程式如下:

 

#include "soapH.h" 
#include "calc.nsmap" 
main() 

   struct soap *soap = soap_new(); 
   double result; 
   if (soap_call_ns2__add(soap, NULL, NULL, 1.0, 2.0, &result) == SOAP_OK) 
      printf("The sum of 1.0 and 2.0 is %lg\n", result); 
   else
      soap_print_fault(soap, stderr); 
   soap_end(soap); 
   soap_free(soap); 
}

 

另外測試server的方式:

 ./calcserver.cgi < calc.add.res.xml

文章標籤
全站熱搜
創作者介紹
創作者 Person 的頭像
Person

Person

Person 發表在 痞客邦 留言(1) 人氣(517)