博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webservice gsoap 小记
阅读量:6988 次
发布时间:2019-06-27

本文共 3858 字,大约阅读时间需要 12 分钟。

参考 

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     else17         service.soap_stream_fault(std::cerr);18     service.destroy();19 20     getchar();21     return 0;22 }

  编译

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

  参考 

  在 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 mappingsint 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

  参考 

  用 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();

 

   以上代码,有待优化。

 

转载于:https://www.cnblogs.com/hangj/p/3620406.html

你可能感兴趣的文章
get荣耀YOYO智能音箱打电话的新技能 感觉赚了1个亿
查看>>
明明长得一样,怎么就不认识你了呢
查看>>
支付宝风控闹“乌龙”,网友却纷纷点赞叫好
查看>>
2017年度华为感动之星巅峰之旅活动完美收官
查看>>
高瓴资本大幅减持虎牙:半年时间闪进闪出
查看>>
亚马逊CEO贝索斯离婚:拥有身家1369亿美元 已结婚25年
查看>>
这一次升级等了七年 PCIe 4.0终于发布了
查看>>
发改委:以稳汽车消费来托住商品消费“大头”
查看>>
华夏幸福前锋拉维奇旧伤复发 已顺利完成手术
查看>>
Python入门:Python 2与Python3有什么区别?
查看>>
淘宝天猫购物再不要这样子了我刚亏了8块钱
查看>>
如何跟踪信息流广告转化数据?
查看>>
美拍直播首屏耗时减少50%以上的优化实践
查看>>
一篇文章搞懂 Activity 启动模式
查看>>
你不知道的 Electron (二):了解 Electron 打包
查看>>
Flexbox Guide
查看>>
听说你精通原生JavaScript,来试试这份题目吧
查看>>
蚂蚁金服自主研发的三地五中心异地多活解决方案获金融科技创新大奖
查看>>
junit测试之第三方组件mock
查看>>
sitemesh的script提取
查看>>