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:
#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 小记的更多相关文章
- C++访问WebService gSoap方式
一. gSOAP访问WebService 1. 下载gSOAP gSOAP 2.7.17 版下载地址http://sourceforge.net/projects/g ...
- 转--webservice、socket、http 小记(一)
webservice.socket.http 小记(一) http://blog.csdn.net/m_123hj_520/article/details/9370723 2013-07-18 17: ...
- 使用GSoap开发WebService客户端与服务端
Gsoap 编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现, 从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多. 用gsoap开发web service的大致思路 我 ...
- GSoap的使用(调用webservice接口)
由于本人写项目时使用到C++要调用C#写得后台服务器发布的webservice,因此抽出来了一点时间整理相关的知识如下 gSOAP是一个绑定SOAP/XML到C/C++语言的工具,使用它可以简单快速地 ...
- Linux下gsoap实现webservice功能
蓝字为关键字,等号=后面为关键字值. 一.介绍 我们用的webservice是根据gsoap编译工具来实现,gSOAP的编译器能够自动的将用户定义的本地化的C或C++数据类型转变为符合XML语法的数据 ...
- Qt+gsoap调用WebService
1. 前言 Qt本身给我们提供了调用WebService的解决方案qsoap,看了一下他的介绍,感觉实在是太弱了,而且又是个新出的东西,所以还是决定不用他.既然使用Qt,那当然是跨平台的解 ...
- gsoap创建webservice服务简单教程
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] WebServicesoapgsoap 使用gsoap创建webservice服务 下载gsop 准备待导出的服务接口定义文件比 ...
- C++客户端通过gSOAP调用WebService
webService三要素: SOAP(Simple Object Access Protocol).WSDL(WebServicesDescriptionLanguage).UDDI(Univers ...
- 使用webService时,gsoap数据类型注意事项
今天在使用gsoap生成webservice客户端文件时,发现我的参数类型全被改了,比如string型变成了char*,原来有STL的地方也变没了,经过研究发现,原来和我生成的头文件时使用的参数有关, ...
随机推荐
- ubuntu 开机自动挂载nfs服务器上的home分区
通过‘fstab’也可以配置 NFS 和 SMB 的共享目录.由于涉及到的可选项很重要,并且需要了解一些协议的工作情况,您得先阅读 Samba 和 NFS . 基本语法和本地介质相差不是很多.条目中的 ...
- Laravel Tinker 使用笔记
我们知道,Laravel Tinker 提供了命令行式的交互调试途径.使用极其方便直观. 使用: #php artisan tinker 要点: 命令要在一行上输入完成,回车执行.>>&g ...
- process概念
multiprocess: multiprocess.cpu_count():统计cpu核数 multiprocess.active_chirdren():获取所有的子进程 multiprocess. ...
- centos / debian 安装iptables服务
debian: #使用可参考 https://www.debian.cn/archives/991 #配置文件位于 /etc/iptables #重新配置使用dpkg-reconfigure ipta ...
- 解决JS中missing ( before function parameters的错误
在编写javascript中,常出现在function处提示“missing ( before function parameters”的错误,这是怎么回事? 例如: function String. ...
- MySQL的left on 【zt】
MySQL的left on [zt] (2008-11-03 17:27:30) 转载▼ 标签: it 分类: 学习笔记 MySQL多表连接查询Left Join,Right Join php开源嘛 ...
- Maximum profit of stocks
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- 2018.07.22 洛谷P4316 绿豆蛙的归宿(概率dp)
传送门 简单的递推. 由于是DAG" role="presentation" style="position: relative;">DAGDA ...
- 着重基础之—MySql Blob类型和Text类型
着重基础之—MySql Blob类型和Text类型 在经历了几个Java项目后,遇到了一些问题,在解决问题中体会到基础需要不断的回顾与巩固. 最近做的项目中,提供给接口调用方数据同步接口,传输的数据格 ...
- Spring MVC controller 被执行两次
interceptor 被执行两次 后来发现 时controller被执行两次 后来发现是jsp页面有个: <img src="#" > 导致被执行两次. 解决方案:去 ...