在windows下使用gSOAP技术调用WebService,使用vc开发

来源:互联网 发布:宝马微平台源码 编辑:程序博客网 时间:2024/05/11 02:32

1、环境搭建

     操作系统:Vista Home

    gSOAP:2.7.17Stable版(下载地址:http://sourceforge.net/projects/gsoap2/)

    c/c++集成环境:VC6.0

 

2、gSOAP

    下载得到gsoap_2.7.17.zip文件,解压该文件。稍后需要从该文件夹中获取相关文件。设释放路径为"G:/webservice/gsoap-2.7",后文%GSOAP代表该文件夹路径。

 

3、通过WSDL文档,生成C/C++头文件

     3.1 WSDL是什么:WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些服务访问点对包含面向文档信息或面向过程调用的服务进行访问(类似远程过程调用)。WSDL首先对访问的操作和访问时使用的请求/响应消息进行抽象描述,然后将其绑定到具体的传输协议和消息格式上以最终定义具体部署的服务访问点。相关的具体部署的服务访问点通过组合就成为抽象的Web服务。

     3.2 通过WSDL生成访问接口:(http://192.168.100.44:8888/kms_provincial/services/LoginInit?wsdl为一WebService接口)

     设定c/c++工程名称为TestWebService(Console程序,其他应用程序类型也可),在该文件夹下面建立TestWebService.h文件。

     启动cmd,进入到%GSOAP/bin目录,调用wsdl2h.exe程序生成TestWebService.h头文件接口定义。

 

     wsdl2h -s –o x:/xxx/TestWebService.h [url]http://192.168.100.44:8888/kms_provincial/services/LoginInit?wsdl[/url]

     执行完毕后,生成soapLoginInitSoapBindingProxy.h、soapLoginInitSoapBindingProxy.cpp文件。

 

    3.3 解析TestWebService.h文件,生成存根程序

         soapcpp2 –C x:/xxx/TestWebService.h

         命令中参数-c代表生成标准C程序,若没有这个参数,则生成C++程序。

        命令中参数-C代表仅生成客户端程序,若没有这个参数,则默认生成客户端和服务端程序。4

 

4 建立vc工程

    

    建立以TestWebService为名称的console工程,将%GSOAP下的soapC.cpp、soapH.h、soapcpp2.h、stdsoap2.cpp、stdsoap2.h、soapStub.h、soapLoginInitSoapBindingProxy.cpp、soapLoginInitSoapBindingProxy.h加入到工程中。

在TestWebService.cpp主工程中加入接口操作代码。

#include "soapH.h" // or whatever it is called, you must already have it
#include "soapLoginInitSoapBindingProxy.h"
#include "LoginInitSoapBinding.nsmap" // this is what you have to add to fix the problem
int main(int argc, char* argv[])
{
 printf("Hello World!/n");
 
 DWORD dwStartTime;
 DWORD dwEndTime;
 
 struct soap clientSOAP;
 LoginInitSoapBindingProxy oLoginInitSoapInterface;
 char *pbRetuned = "";
 int iRet = 0;
 dwStartTime = GetTickCount();
 
 for (int i= 0; i<1000; i++)
 {
  soap_init(&clientSOAP);
  
  iRet = oLoginInitSoapInterface.service(pbRetuned);
  
#ifdef _DEBUG
  printf("result: oLoginInitSoapInterface.service = %d/r/n", iRet);
  printf("value = %s/r/n", pbRetuned);
#endif
  
  soap_destroy(&clientSOAP);
  soap_end(&clientSOAP);
  soap_done(&clientSOAP);
 }
 
 dwEndTime = GetTickCount();
 printf("result: oLoginInitSoapInterface.service = %d/r/n", iRet);
 printf("value = %s/r/n", pbRetuned);
 printf("elaspe time = %d/r/n", (dwEndTime - dwStartTime)/i);
 

 return 0;
}

      在该工程中,service为WebService上的一个自定义接口,返回值为pbRetuned。

 

5、注意问题

    5.1 在工程头文件中加入#include "LoginInitSoapBinding.nsmap" // this is what you have to add to fix the problem,否则会有编译问题,请自己尝试;

   5.2 中文汉字问题,注意编码问题,统一即可。

 

6、linux实现可参考该博客实现:http://commandos.blog.51cto.com/154976/130652

7、代码:整个代码工程在附件中。(下载附件,修改后缀为.rar后,解压即可)

      http://hi.csdn.net/attachment/201005/10/0_1273477890fHW2.gif