参考 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. Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别

    原文链接:http://www.cnblogs.com/signheart/p/922dcf75dd5fe6b418d4475af89c4664.html 使用Thread.currentThread ...

  2. html5之history对象理解

    history对象之pushState,replaceState浏览器有一个history对象,用来保存浏览历史,用户可以通过点击浏览器的后退或前进按钮在历史记录中切换.之前对history的操作的A ...

  3. 2018.09.11 loj#10216.五指山(exgcd)

    传送门 就是一个exgcd的板子. 但注意算距离差的时候是在一个环上面算. 还有,答案要开long long233... 注意这两点之后就是exgcd板子了. 代码: #include<bits ...

  4. Django入门与实践-第25章:Markdown 支持(完结)

    http://127.0.0.1:8000/boards/1/topics/102/reply/ 让我们在文本区域添加 Markdown 支持来改善用户体验. 你会看到要实现这个功能非常简单. 首先, ...

  5. Java 原始类型JComboBox的成员JComboBox(E())的调用 未经过检查

    问题描述: 根据书上的代码 ,编译时候出现以下问题 自定义了一个字符数组: private String[] grades = {"1","2","3 ...

  6. Hdu1342 Lotto 2017-01-18 17:12 44人阅读 评论(0) 收藏

    Lotto Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  7. 一个简单 Go Web MVC 框架实现思路

    需要的知识点 为了防止你的心里不适,需要以下知识点: Go 基本知识 Go 反射的深入理解 使用过框架 Go Web 服务器搭建 package main import ( "fmt&quo ...

  8. springmvc 开涛 拦截器

    拦截器有三个方法:preHandle, postHandle, afterCompletion ***-servlet.xml <bean name="/test" clas ...

  9. onclick传参

    var tema="<a title='打开' href='javascript:;' onclick='showKnowledgeMap(1,\" "+kl_na ...

  10. 我的成长比价系列:java web开发过程中遇到的错误一:sql语句换行错误

    字符串换行导致的错误,确切的说是马虎的错误,自己在编写简单的servlet项目时,在StudentDao.java 中的  查询语句:String  sql= "SELECT Type,fl ...