参考 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:

 #include "soapWebService1SoapProxy.h"    // 自动生成的头文件 注:不用MyHead.h
#include "WebService1Soap.nsmap" int main()
{
WebService1SoapProxy service;
_tempuri__GetSum a;
_tempuri__GetSumResponse result;
//a.soap = soap_new();
a.a = ;
a.b = ;
//result.soap = soap_new(); if (service.GetSum(&a, &result) == SOAP_OK)
std::cout << "the num of 1 and 5 is " << result.GetSumResult << std::endl;
else
service.soap_stream_fault(std::cerr);
service.destroy(); getchar();
return ;
}

  编译

  如果遇到 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)

  

 // File: currentTime.h
//gsoap ns service name: currentTime
//gsoap ns service namespace: urn:currentTime
//gsoap ns service location: http://www.yourdomain.com/currentTime.cgi
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() != SOAP_TCP_ERROR)
{
server.soap_stream_fault(std::cerr);
} return ;
} int currentTimeService::currentTime(time_t& response)
{
response = time();
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 的初始化函数:

     public picture() {
this.Url = "http://localhost:65432/picture.cgi"; // 改成你的地址
}

  OK,可以用了。

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

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

  

 int pictureService::picture(struct picdata &picture)
{
char *pic = NULL;
unsigned int length = ; std::ifstream is("chicken.jpg", std::ios::binary);
if (is)
{
is.seekg(, is.beg);
is.seekg(, is.end);
length = is.tellg();
is.seekg(, is.beg); pic = new char[length]; if (pic == NULL)
return SOAP_ERR; is.read(pic, length);
is.close();
} picture.data = base64_encode((unsigned char*)pic, length); if (pic != NULL)
{
delete pic;
pic = NULL;
} return SOAP_OK;
}

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

     picture pic = new picture();
picture1 pic1 = new picture1();
pictureResponse res = pic.Callpicture(pic1); string strData = res.picture.data;
byte[] bytes = Convert.FromBase64String(strData); FileStream fs = new FileStream("D:\\picture.jpg", FileMode.OpenOrCreate);
fs.Write(bytes, , bytes.Length);
fs.Close();

  以上代码,有待优化。

webservice gsoap 小记的更多相关文章

  1. C++访问WebService gSoap方式

    一.             gSOAP访问WebService 1.      下载gSOAP gSOAP 2.7.17 版下载地址http://sourceforge.net/projects/g ...

  2. 转--webservice、socket、http 小记(一)

    webservice.socket.http 小记(一) http://blog.csdn.net/m_123hj_520/article/details/9370723 2013-07-18 17: ...

  3. 使用GSoap开发WebService客户端与服务端

    Gsoap 编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现, 从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多. 用gsoap开发web service的大致思路 我 ...

  4. GSoap的使用(调用webservice接口)

    由于本人写项目时使用到C++要调用C#写得后台服务器发布的webservice,因此抽出来了一点时间整理相关的知识如下 gSOAP是一个绑定SOAP/XML到C/C++语言的工具,使用它可以简单快速地 ...

  5. Linux下gsoap实现webservice功能

    蓝字为关键字,等号=后面为关键字值. 一.介绍 我们用的webservice是根据gsoap编译工具来实现,gSOAP的编译器能够自动的将用户定义的本地化的C或C++数据类型转变为符合XML语法的数据 ...

  6. Qt+gsoap调用WebService

    1.       前言 Qt本身给我们提供了调用WebService的解决方案qsoap,看了一下他的介绍,感觉实在是太弱了,而且又是个新出的东西,所以还是决定不用他.既然使用Qt,那当然是跨平台的解 ...

  7. gsoap创建webservice服务简单教程

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] WebServicesoapgsoap 使用gsoap创建webservice服务 下载gsop 准备待导出的服务接口定义文件比 ...

  8. C++客户端通过gSOAP调用WebService

    webService三要素: SOAP(Simple Object Access Protocol).WSDL(WebServicesDescriptionLanguage).UDDI(Univers ...

  9. 使用webService时,gsoap数据类型注意事项

    今天在使用gsoap生成webservice客户端文件时,发现我的参数类型全被改了,比如string型变成了char*,原来有STL的地方也变没了,经过研究发现,原来和我生成的头文件时使用的参数有关, ...

随机推荐

  1. db2 查看表空间使用率

    1. 统计所有节点表空间使用率 select substr(TABLESPACE_NAME,1,20) as TBSPC_NAME,bigint(TOTAL_PAGES * PAGE_SIZE)/10 ...

  2. MariaDB · 版本特性 · MariaDB 的 GTID 介绍

    本文来自阿里的数据库内核月报,写的很详细,主要是关于mariadb开启gtid之后做主从的方法. 原文连接:http://mysql.taobao.org/monthly/2016/02/08/

  3. vue run dev 8080端口被占用

    用vue 官方脚手架vue-cli构建项目容易碰到一些小错误 vue init webpack project-name ...... cd project-name npm install npm ...

  4. org.apache.commons札记

    StringUtils.isBlank(null); //trueStringUtils.isBlank(""); //trueStringUtils.isBlank(" ...

  5. Android 控制ScrollView滚动到底部或顶部

    在开发中,我们经常需要更新列表,并将列表拉倒最底部,比如发表微博,聊天界面等等, 这里有两种办法,第一种,使用scrollTo(): public static void scrollToBottom ...

  6. An integration of deep learning and neuroscience

    Recently, I have read a paper about the integration of deep learing and neuroscience, which elaborat ...

  7. Reverse string using recursion

    On-Site Question 3 - SOLUTION Question Given a string, write a function that uses recursion to rever ...

  8. 2018.10.09 NOIP模拟 好数(双向搜索)

    传送门 直接双向搜索出两边可行解,然后把两边的可行解合并起来得出答案就行了. 注意合并的时候可以利用排序和单调性优化时间复杂度. 直接枚举合并是O(siza∗sizb)O(siza*sizb)O(si ...

  9. Win7下U盘安装CentOS-7-x86_64-DVD-1503-01

    转载自:http://blog.sina.com.cn/s/blog_842d5c8a0102vr12.html 昨天在Win7下装CentOS7,本以为之前装CentOS6.5很熟练了,结合网上的帖 ...

  10. mac下svn无法上传.a文件的问题

    Xcode自带的svn和Versions以及一些其它工具都默认ignore".a"文件. 解决办法有两个: 方法一:使用命令行添加文件([转]原文在这) 1.打开终端,输入cd,空 ...