天天看点

gsoap进行soap开发 1 通过wsdl文件生成.h文件 2 由.h文件生成所需要的cpp/c文件 3 client例子 4 server例子

1 通过wsdl文件生成.h文件

从WSDL中产生头文件

用法:

wsdl2h -o 头文件名 WSDL文件名或URL      

wsdl2h常用选项

  • -o 文件名,指定输出头文件
  • -n 名空间前缀 代替默认的ns
  • -c 产生纯C代码,否则是C++代码
  • -s 不要使用STL代码
  • -t 文件名,指定type map文件,默认为typemap.dat
  • -e 禁止为enum成员加上名空间前缀

type map文件用于指定SOAP/XML中的类型与C/C++之间的转换规则,比如在wsmap.dat里写

2 由.h文件生成所需要的cpp/c文件

用法

soapcpp2 头文件      

例:

soapcpp2 ayandy.h      

将生成下面这些文件

  • soapStub.h    // soap的存根文件,定义了ayandy.h里对应的远程调用模型
  • soapC.c soapH.h  // soap的序列和反序列代码,它已经包含了soapStub.h,服务器端与客户端都要包含它
  • soapClient.c soapClientLib.c // 客户端代码,soapClientLib.c文件则只是简单地包含soapClient.c和soapC.c
  • soapServer.c soapServerLib.c // 服务器端代码,soapServerLib.c文件则只是简单地包含soapServer.c和soapC.c
  • ServiceSoap.nsmap ServiceSoap12.nsmap // 名空间定义,服务器端与客户端都要包含它
  • soapServiceSoapProxy.h soapServiceSoap12Proxy.h // 客户端的C++简单包装(如果头文件是纯C代码,这两个文件就不会生成)

综上所述

  • 如果编写服务器端,项目里应该加入soapServerLib.c,代码里包含头文件soapH.h
  • 如果编写客户端,项目里应该加入soapClientLib.c,代码里包含头文件SoapH.h(或xxxxProxy.h)
  • 当然,还要加入gsoap库里的stdsoap2.cpp文件(如果是写C代码,则加入stdsoap2.c)

如果看到soapcpp2提示:”Critical error: #import: Cannot open file "stlvector.h" for reading.“, 那是因为我们的头文件使用了STL(wsdl2h 没用-s选项),这时要使用-I选项指定gSOAP的 import文件路径,这个路径是"$gsoap\gsoap\import":

soapcpp2 ayandy.h -I \path\gsoap\import      

soapcpp2常用选项

  • -C 仅生成客户端代码
  • -S 仅生成服务器端代码
  • -L 不要产生soapClientLib.c和soapServerLib.c文件
  • -c 产生纯C代码,否则是C++代码(与头文件有关)
  • -I 指定import路径(见上文)
  • -x 不要产生XML示例文件
  • -i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。

------------------------------------------

工程中所要包含的文件(不是include的)有:

soapStub.h  soapH.h     stdsoap2.h      AbysalEmail.h (这个文件时第一步生成的头文件)

            soapC.cpp   stdsoap2.cpp    soapClient.cpp (客户端) soapServer.cpp(服务器端)

程序中include的头文件有

#include "soap/ServiceSoap.nsmap"//命名空间,这个必不可少

#include "soap/soapH.h

#include "soap/soapServiceSoapProxy.h" //如果使用代理类

调用哪些方法 可以去 soapStub.h 去找

3 client例子

首先用wsdl2h生成头文件,URL可以先用http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl这个。使用-c参数来生成纯c代码。

wsdl2h -c -o quote.h http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl

生成的quote.h为:

//gsoap ns1 service name: net_DOTxmethods_DOTservices_DOTstockquote_DOTStockQuoteBinding 
//gsoap ns1 service type: net_DOTxmethods_DOTservices_DOTstockquote_DOTStockQuotePortType 
//gsoap ns1 service port: http://66.28.98.121:9090/soap 
//gsoap ns1 service namespace: urn:xmethods-delayed-quotes 
//gsoap ns1 service documentation: Definitions generated by the gSOAP WSDL parser 1.0 
// Service net.xmethods.services.stockquote.StockQuoteService : net.xmethods.services.stockquote.StockQuote web service 
//gsoap ns1 service method-style: getQuote rpc 
//gsoap ns1 service method-encoding: getQuote http://schemas.xmlsoap.org/soap/encoding/ 
//gsoap ns1 service method-action: getQuote urn:xmethods-delayed-quotes#getQuote 
int ns1__getQuote(char *symbol, float &Result);
           

然后使用soapcpp2命令,来生成c代码文件。

soapcpp2 -c quote.h

将会生成如下方法:

int soap_call_ns1__getQuote(struct soap *soap, char *URL, char *action, char *symbol, float &Result);

客户端可以如下编写:

#include "soapH.h" // obtain the generated stub  
#include "Quote.nsmap" // obtain the generated XML namespace mapping table for the Quote service  
main()  
{  
   struct soap *soap = soap_new(); 
   float quote;  
   if (soap_call_ns1__getQuote(soap, NULL, NULL, "IBM", quote) == SOAP_OK)  
      printf("Current IBM Stock Quote = %g\n", quote); 
   else // an error occurred  
      soap_print_fault(soap, stderr); // display the SOAP fault on the stderr stream  
}
           

c++客户端可以使用代理类来写代码:

#include "soapQuoteProxy.h" // get proxy 
#include "Quote.nsmap" // get namespace bindings 
int main() 
{ 
   Quote q; 
   float r; 
   if (q.ns1__getQuote("IBM", r) == SOAP_OK) 
      std::cout << r << std::endl; 
   else
      soap_print_fault(q.soap, stderr); 
   return 0; 
}
           

4 server例子

首先自己写头文件calc.h,该服务器提供三个方法:计算加法,减法,平方。

// Contents of file "calc.h":  
//gsoap ns service name: calculator 
//gsoap ns service style: rpc 
//gsoap ns service encoding: encoded 
//gsoap ns service port: http://mydomain/path/calculator.cgi 
//gsoap ns service namespace: urn:calculator 
int ns__add(double a, double b, double &result);  
int ns__sub(double a, double b, double &result);  
int ns__sqrt(double a, double &result);
           

然后使用soapcpp2命令来生成源文件。

soapcpp2 calc.h

编写提供方法的实现,calc.cpp文件:

// Contents of file "calc.cpp":  
#include "soapH.h"  
#include "calculator.nsmap"  
#include <math.h>  
main()  
{  
   soap_serve(soap_new()); // call the incoming remote method request dispatcher  
}  
// Implementation of the "add" remote method:  
int ns__add(struct soap *soap, double a, double b, double &result)  
{  
   result = a + b;  
   return SOAP_OK;  
}  
// Implementation of the "sub" remote method:  
int ns__sub(struct soap *soap, double a, double b, double &result)  
{  
   result = a - b;  
   return SOAP_OK;  
}  
// Implementation of the "sqrt" remote method:  
int ns__sqrt(struct soap *soap, double a, double &result)  
{  
   if (a >= 0)  
   {  
      result = sqrt(a);  
      return SOAP_OK;  
   }  
   else 
   {  
      return soap_sender_fault(soap, "Square root of negative value", "I can only compute the square root of a non-negative value"); 
   }  
} 
           

最后编译calc.cpp soapServerLib.cpp 生成server服务器。

继续阅读