webservice gsoap 小记

参考 http://www.cs.fsu.edu/~engelen/soap.html

1. web service client application

  > wsdl2h -s -o MyHead.h http://www.genivia.com/calc.wsdl

    -s 不用 STL

  > soapcpp2 -i -C MyHead.h

    Option -i (and alternatively option -j) indicates that we want C++ proxy and server objects that include the client (and server) code, -C indicates client-side only 

    soapcpp2 generates both client and server stubs and skeletons by default

  

  这两步过程中可能会出错,要把必要的文件加上: soap12.h  stdsoap2.h  stdsoap2.cpp

  然后,We use the generated soapcalcProxy class and calc.nsmap XML namespace mapping table to access the Web service. The soapcalcProxy class is a proxy to invoke the service:

 1 #include "soapWebService1SoapProxy.h"    // 自动生成的头文件 注:不用MyHead.h

 2 #include "WebService1Soap.nsmap"

 3 

 4 int main()

 5 {

 6     WebService1SoapProxy        service;

 7     _tempuri__GetSum            a;

 8     _tempuri__GetSumResponse    result;

 9     //a.soap = soap_new();

10     a.a = 1;

11     a.b = 5;

12     //result.soap = soap_new();

13     

14     if (service.GetSum(&a, &result) == SOAP_OK)

15         std::cout << "the num of 1 and 5 is " << result.GetSumResult << std::endl;

16     else

17         service.soap_stream_fault(std::cerr);

18     service.destroy();

19 

20     getchar();

21     return 0;

22 }

  编译

  如果遇到 stdsoap2.obj : error LNK2001: 无法解析的外部符号_namespaces

  参考 http://blog.163.com/lyz_sea/blog/static/115586707201182432412677

  在 stdsoap2.h,添加 

  #ifndef WITH_NONAMESPACES
  #define WITH_NONAMESPACES
  #endif

 

2. Develop a Web Service (Stand-Alone Server)

  

1 // File: currentTime.h 

2 //gsoap ns service name: currentTime 

3 //gsoap ns service namespace: urn:currentTime 

4 //gsoap ns service location: http://www.yourdomain.com/currentTime.cgi 

5 int ns__currentTime(time_t& response);

  > soapcpp2 -i -S currentTime.h

  

// File: currentTime.cpp

#include "soapcurrentTimeService.h" // include the proxy declarations

#include "currentTime.nsmap" // include the XML namespace mappings

int main()

{

    // create server and serve on CGI-based request:

    currentTimeService    server;

    /*server.serve();

    server.destroy();

    */



    while (server.run(65533) != SOAP_TCP_ERROR)

    {

        server.soap_stream_fault(std::cerr);

    }



    return 0;

}



int currentTimeService::currentTime(time_t& response)

{

    response = time(0);

    return SOAP_OK;

}

  Compile with  currentTime.cpp  soapC.cpp  soapcurrentTimeService.cpp  stdsoap2.cpp

  

  run

  然后可以由 wsdl 文件生成客户端头文件,and ...

 

3. C# 调用 gsoap 的 service

  参考 http://blog.sina.com.cn/s/blog_4e7d38260100ade4.html

  用 VS 自带的 wsdl.exe (在 C 盘的某个角落)

  > wsdl.exe currentTime.wsdl

  生成 currentTime.cs

  将 currentTime.cs 放到 C# 的工程中,研究下代码结构,找到 currentTime 的初始化函数:

1     public picture() {

2         this.Url = "http://localhost:65432/picture.cgi"; // 改成你的地址  

3     }

  OK,可以用了。

 

4. 如果要发送 二进制数据,可先将其进行编码(暂时采用 base64 编码,实际场景中应采用更合适的编码算法),以 string 形式发送。接收端再进行相应解码。

  C++ gsoap service 端,读取图片:

  

 1 int pictureService::picture(struct picdata &picture)

 2 {

 3     char *pic = NULL;

 4     unsigned int length = 0;

 5 

 6     std::ifstream is("chicken.jpg", std::ios::binary);

 7     if (is)

 8     {

 9         is.seekg(0, is.beg);

10         is.seekg(0, is.end); 

11         length = is.tellg();

12         is.seekg(0, is.beg);

13 

14         pic = new char[length];

15 

16         if (pic == NULL)

17             return SOAP_ERR;

18         

19         is.read(pic, length);

20         is.close();

21     }

22 

23     picture.data = base64_encode((unsigned char*)pic, length);

24 

25     if (pic != NULL)

26     {

27         delete pic;

28         pic = NULL;

29     }

30 

31     return SOAP_OK;

32 }

 

  C# 端,调用 webservice,获取一张图片,并保存:

 1     picture pic = new picture();

 2     picture1 pic1 = new picture1();

 3     pictureResponse res = pic.Callpicture(pic1);

 4 

 5     string strData = res.picture.data;

 6     byte[] bytes = Convert.FromBase64String(strData);

 7 

 8     FileStream fs = new FileStream("D:\\picture.jpg", FileMode.OpenOrCreate);

 9     fs.Write(bytes, 0, bytes.Length);

10     fs.Close();

 

   以上代码,有待优化。

 

你可能感兴趣的:(webservice)