参考 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. spring boot2.03 spring cloud Finchley.RELEASE遇到的问题

    1.spring cloud bus 本地不能更新 原因是@RefreshScope注解要加在需要更新的controller上 2.No instances found of configserver ...

  2. Mysql的内存优化

     老师  vi mysqld_safe# executing mysqld_safe 后面增加export LD_PRELOAD=/usr/local/lib/libtcmalloc.so  可以做一 ...

  3. 转载 springboot 配置读取

    前言:了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ...

  4. Python使用wxPython、py2exe编写桌面程序-乾颐堂

    Python是支持可视化编程,即编写gui程序,你可以用它来编写自己喜欢的桌面程序.使用wxPython来做界面非常的简单,只是不能像C#一样拖动控件,需要自行写代码布局.在完成编写之后,由于直接的p ...

  5. CreateThread

    CreateThread(NULL,0,ReportResultThread,this,0,&g_dwThreadId) 2. 参数说明: 第一个参数 lpThreadAttributes 表 ...

  6. makefile all

    all:udps udpc udps:udpserv.c    gcc -Wall -o udps udpserv.cudpc:udpclient.c    gcc -Wall -o udpc udp ...

  7. 为什么c++中返回成员变量的指针,会破坏了封装?

    上述代码中,get()函数返回的是类成员变量的name的地址,这是很危险的,name是私有的,意味这不想被客户访问,但是如果返回name的地址,那么外部函数就可以修改name,这就破坏了封装性. 为什 ...

  8. 2018.09.24 codeforces 1051F. The Shortest Statement(dijkstra+lca)

    传送门 这真是一道一言难尽的题. 首先比赛的时候居然没想出来正解. 其次赛后调试一直调不出来最后发现是depth传错了. 其实这是一道简单题啊. 对于树边直接lca求距离. 由于非树边最多21条. 因 ...

  9. Win7 SP1 提示ADO的问题

    需要安装  Windows6.1-KB2640696-v3-x64.msu 这个Pack

  10. com.liuyang.exception.DaoException: java.sql.SQLException: Incorrect string

    错误是在JUnit测试的时候产生的,但是实际加入数据也会产生这样的情况,主要是数据库内部的编码方式不支持当前的编码方式导致的冲突,解决方法就是在建立数据库之前,要查看当前的数据库的编码方式,方法和更改 ...