ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Hello, gSoap: 用 gSoap 创建自己的第一个 Web Services 程序。

2021-05-29 17:31:57  阅读:232  来源: 互联网

标签:Web Saving string cpp gSoap Services ns hello


目录

1. 准备项目文件夹。

先创建一个文件夹保存我们这个项目的文件。

stdsoap2.cpp 文件从 gSoap 的文件夹里复制到当前文件夹,等一会编译项目的时候要用。其实可以用更优雅的方法,比如设置搜索路径等。本文中,我就粗暴处理了,直接复制过来,当然这样也无伤大雅,

2. 创建源代码

头文件 hello.h

int ns__hello(std::string name, std::string& greeting);

代码文件 hello.cpp

#include "soapH.h"  // include the generated source code headers
#include "ns.nsmap" // include XML namespaces
int main()
{
  return soap_serve(soap_new());
}
int ns__hello(struct soap *soap, std::string name, std::string& greeting)
{
  greeting = "Hello " + name;
  return SOAP_OK;
}

你可能觉得 hello.h 里面竟然没有引用 C++ 头文件 string 就用了 std::string,其实并无影响。因为在代码文件中引用头文件 soapH.h 中会把这些都处理妥当的。

3. 生成 Web Services 源代码

那么代码文件中的 soapH.hns.nsmap 这两个头文件如何得到呢?这个很容易,可以输入命令行 soapcpp2 hello.h 自动生成:

$ soapcpp2 hello.h

**  The gSOAP code generator for C and C++, soapcpp2 release 2.8.114
**  Copyright (C) 2000-2021, Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The soapcpp2 tool and its generated software are released under the GPL.
**  ----------------------------------------------------------------------------
**  A commercial use license is available from Genivia Inc., contact@genivia.com
**  ----------------------------------------------------------------------------

Saving soapStub.h annotated copy of the source interface header file
Saving soapH.h serialization functions to #include in projects
Using ns service name: Service
Using ns service style: document
Using ns service encoding: literal
Using ns schema namespace: http://tempuri.org/ns.xsd
Saving ns.wsdl Web Service description
Saving ns.hello.req.xml sample SOAP/XML request
Saving ns.hello.res.xml sample SOAP/XML response
Saving ns.xsd XML schema
Saving ns.nsmap namespace mapping table
Saving soapClient.cpp client call stub functions
Saving soapClientLib.cpp client stubs with serializers (use only for libs)
Saving soapServer.cpp server request dispatcher
Saving soapServerLib.cpp server request dispatcher with serializers (use only for libs)
Saving soapC.cpp serialization functions

Compilation successful

4. 编译项目

用下面命令编译生成目标代码 hello.cgi 程序。

g++ -o hello.cgi hello.cpp soapC.cpp soapServer.cpp stdsoap2.cpp

生等片刻,看一下当前文件夹,多出了一个新文件 hello.cgi

5. 部署

这个 cgi 文件,最简单的方法是部署在现成的 Web 服务器里面,比如 Apache 或 IIS 这类软件。

当然,也可以不借助外援,直接用 C 或 C++ 自己实现 Web 服务功能。我喜欢这种方法,回头专门写文章讨论这种方法。

本文没有进行部署,所以测试也就免了。感兴趣的朋友,看我的下一篇文章吧。


参考资料

标签:Web,Saving,string,cpp,gSoap,Services,ns,hello
来源: https://blog.csdn.net/quicmous/article/details/117385274

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有