gsoap开发webservice 服务端.md

无WSDL文件

1.编写头文件websever2.h:

 1 //gsoap ns service name: calc
 2 //gsoap ns service style: rpc
 3 //gsoap ns service encoding: encoded
 4 //gsoap ns service namespace: http://127.0.0.1:8089/calc.wsdl
 5 //gsoap ns service location: http://127.0.0.1:8089/cal
 6 //gsoap ns schema  namespace:    urn:calc
 7 int ns__add(double a, double b, double *result);
 8 int ns__sub(double a, double b, double *result);
 9 int ns__mul(double a, double b, double *result);
10 int ns__div(double a, double b, double *result);
11 int ns__pow(double a, double b, double *result);

2.生成文件:

1 C:\Users\admin>e:
2 E:\>cd E:\4.0projet\webserver2\gsoap_2.9.65\gsoap-2.8\gsoap\bin\win32
3 E:\4.0projet\webserver2\gsoap_2.9.65\gsoap-2.8\gsoap\bin\win32>soapcpp2.exe -i -2 webserver2.h

3.将以下文件拷如项目:

前6个路径:
E:\4.0project\webserver2\gsoap_2.8.65\gsoap-2.8\gsoap\bin\win32

cal.nsmap
soapC.cpp
soapcalService.cpp(名称不固定,以service结尾)
soapcalcService.h(名称不固定,以service结尾)
soapH.h
soapStub.h

以下文件在路径:
E:\4.0project\webserver2\gsoap_2.8.65\gsoap-2.8\gsoap

stdsoap2.h
stdsoap2.cpp

分别添加到Header Files、Resource Files、Source Files内

4.编写gSOAPService.cpp:

  1 #define _CRT_SECURE_NO_WARNINGS   **//一定要添加上**
  2 #include "calc.nsmap" 
  3 #include"soapcalcService.h"
  4 #include "iostream"  //控件问提只能写“”
  5 
  6 using namespace std;
  7 
  8 //很重要
  9 int   http_get(struct   soap   *soap)
 10 {
 11     FILE*fd = NULL;
 12     fd = fopen("E:\\4.0project\\webserver2\\gsoap_2.8.65\\gsoap-2.8\\gsoap\\bin\\win32\\calc.wsdl", "rb"); //open WSDL file to copy
 13 
 14     if (!fd)
 15     {
 16         return 404; //return HTTP not found error
 17     }
 18     soap->http_content = "text/xml";  //HTTP header with text /xml content
 19     soap_response(soap, SOAP_FILE);
 20     for (;;)
 21     {
 22         size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
 23         if (!r)
 24         {
 25             break;
 26         }
 27         if (soap_send_raw(soap, soap->tmpbuf, r))
 28         {
 29             break; //cannot send, but little we can do about that
 30         }
 31     }
 32     fclose(fd);
 33     soap_end_send(soap);
 34     return SOAP_OK;
 35 }
 36 int main(int argc, char *argv[])
 37 {
 38     calcService cal;
 39     cal.fget = http_get;
 40     while (1)
 41     {
 42         if (cal.run(8089))
 43         {
 44             cal.soap_stream_fault(std::cerr);
 45         }
 46     }
 47     return 0;
 48 }
 49 
 50 //自动生成了calcService类,自己重写add等函数
 51 /*加法的具体实现*/
 52 int calcService::add(double num1, double num2, double* result)
 53 {
 54     if (NULL == result)
 55     {
 56         printf("Error:The third argument should not be NULL!\n");
 57         return SOAP_ERR;
 58     }
 59     else
 60     {
 61         (*result) = num1 + num2;
 62         return SOAP_OK;
 63     }
 64     return SOAP_OK;
 65 }
 66 
 67 /*减法的具体实现*/
 68 int calcService::sub(double num1, double num2, double* result)
 69 {
 70     if (NULL == result)
 71     {
 72         printf("Error:The third argument should not be NULL!\n");
 73         return SOAP_ERR;
 74     }
 75     else
 76     {
 77         (*result) = num1 - num2;
 78         return SOAP_OK;
 79     }
 80     return SOAP_OK;
 81 }
 82 
 83 /*乘法的具体实现*/
 84 int calcService::mul(double num1, double num2, double* result)
 85 {
 86     if (NULL == result)
 87     {
 88         printf("Error:The third argument should not be NULL!\n");
 89         return SOAP_ERR;
 90     }
 91     else
 92     {
 93         (*result) = num1 * num2;
 94         return SOAP_OK;
 95     }
 96     return SOAP_OK;
 97 }
 98 
 99 /*除法的具体实现*/
100 int calcService::div(double num1, double num2, double* result)
101 {
102     if (NULL == result || 0 == num2)
103     {
104         return soap_senderfault("Square root of negative value", "I can only compute the square root of a non-negative value");
105         return SOAP_ERR;
106     }
107     else
108     {
109         (*result) = num1 / num2;
110         return SOAP_OK;
111     }
112     return SOAP_OK;
113 }
114 
115 int calcService::pow(double num1, double num2, double* result)
116 {
117     if (NULL == result || 0 == num2)
118     {
119         printf("Error:The second argument is 0 or The third argument is NULL!\n");
120         return SOAP_ERR;
121     }
122     else
123     {
124         (*result) = num1 / num2;
125         return SOAP_OK;
126     }
127     return SOAP_OK;
128 }

测试:浏览器输入: http://127.0.0.1:8089/calc.wsdl
可以查看wsdl文件 soapUI,新建soap工程,选择对应的wsdl,可以测试使用

已有wsdl文件

部分情况下,客户端已开发完成,需要根据已有wsdl生成.h文件,再通过.h文件生成服务端代码。 根据已有wsdl文件,生成.h文件

D:\JHC00144512\gsoap_2.8.65\gsoap-2.8\gsoap\bin\win32>wsdl2h.exe calc.wsdl

修改wsdl地址时,主要修改wsdl文件内最后一部分中的:

<soap:address location="http://127.0.0.1:8089/macWS"/> 内location信息。


 

原文地址:https://www.cnblogs.com/ouzai/p/11840993.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXObject){xhr=newActiveXObject("Microsoft.XMLHTTP");}else{xhr=newXMLHttpRequest();}//指定请求地址varurl="http://127.0.0.1:7777/hello?wsdl";//
               好不容易把WebService服务器端搭建起来,我们还需要客户端程序也同样跑起来才能够进行和服务器端程序的通信: 在这篇文章里面,我会先自己写代码去实现调用WebService服务器端程序,接下来,通过IDEA配置的方式来调用WebService服务端: 首先,我写了一个W
1新建一个工程项目用来做服务端增加一个MyService1类文件packagecom.zns.ws;importjavax.jws.WebMethod;importjavax.jws.WebService;importjavax.xml.ws.Endpoint;@WebServicepublicclassMyService1{publicstaticvoidmain(String[]args){
packagecom.transsion.util;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.URL;importjava.net.URLConnection;importcom.alibaba.druid.util.Base64;importcom.tra
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Web;5usingSystem.Web.Services.Description;67namespaceStrongSoftManage.Web.App8{9publicclassSoapExtens:SoapExtensi
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生成2个类:ContractConnector.java packagecom.wonders.webservice.contract;importjava.text.DecimalFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;i
一、WebService概述1.1什么是WebService 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 一个跨语言、跨平台的规范(抽象)所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!跨操作系统平台则是指服务
一、什么是CXF?    ApacheCXF=Celtix+Xfire,开始叫ApacheCeltiXfire,后来更名为ApacheCXF了,以下简称为CXF。ApacheCXF是一个开源的webServices框架,CXF帮助您构建和开发webServices,它支持多种协议,比如:SOAP1.1,1,2 XML/HTTP、RESTful或者CORBA。  
工具IDEA一、构建项目1、选择SpringInitializr2、填写项目信息3、勾选webService4、勾选Thymeleaf5、项目建立完成,启动类自动生成二、写个Controller启动服务。浏览器访问/hello接口。 
1.环境win764位,vs20102.新建一个asp.netweb应用程序(同上一篇)3.添加一个web引用,引用上一篇创建的服务。注意不是服务引用。如下图 
WebService之WSDL文件讲解   恩,我想说的是,是不是经常有人在开发的时候,特别是和第三方有接口的时候,走的是SOAP协议,然后用户给你一个WSDL文件,说按照上面的进行适配,嘿嘿,这个时候,要是你以前没有开发过,肯定会傻眼,那如果你想学习的话,就认真的看下面的讲解咯:一、WSDL概述  
在websrvice发布文件的webconfig中加入<httpRuntimemaxRequestLength="102400"/> <webServices>     <protocols>       <addname="HttpPost"/>       <addname="HttpGet"/>     </protocols>   
 代码比较简单,按照如下来操作即可,只是jar包有很多问题,比如找不到classnotFondspring、以及找不到xfile.xml、以及xfile.xml中的一个参数问题,以及2.0 spring。jar和spring1.6.2冲突问题,总之这个小demo报了一堆错误,其实都是jar的问题,为了让大家减少这方面的错误,所以我提供
 一、soapUI简介SOAP:   WebService通过Http协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息头的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SOAP提供了标准的RPC方法来调用WebService。 
参考,感谢https://blog.csdn.net/hj7jay/article/details/727224381.环境:win764位,jdk1.8.0_201 EclipseJavaEEIDEforWebDevelopers.Version:Mars.1Release(4.5.1)2.创建一个普通的java项目,名字是TheService在src目录下创建一个com.hyan.service包,在此包下创建
CXF实现RestfulWebService基础示例一、写在前面IDE:IDEA14JDK:1.7CXF:2.6.2示例来源:%CXF_HOME%\samples\jax_rs\basic发布方式:JAXRSServerFactoryBean的create()方法调用方式:URL的openStream()方法、HttpClient的executeMethod()方法二、服务端(Java项目)1.准备Jar包
封装helper类:classWebServiceHelper{///<summary>///1.get请求http方法///</summary>///<paramname="url">基础url</param>///<paramname="method">请求方法</param>///<paramnam
.net客户端调用java或.netwebservice进行soapheader验证最近项目中有业务需要跨平台调用web服务,客户端和服务器之间采用非对称加密来保证数据的安全性,webservice的安全验证基于soapheader。借此机会,顺便整理一下调用.netwebservice和javawebservice的验证方式,记录下来。
Node.jshttps://www.cnblogs.com/goldlong/p/8027997.htmlQQ音乐apihttps://juejin.im/post/5a35228e51882506a463b172#heading-11?tdsourcetag=s_pcqq_aiomsgGit把本地仓库上传到GitHbubhttps://blog.csdn.net/zamamiro/article/details/70172900git删除本地仓库https://blog.cs
转载自:孤傲苍狼 WebService学习总结(三)——使用JDK开发WebService一、WebService的开发手段使用Java开发WebService时可以使用以下两种开发手段1、 使用JDK开发(1.6及以上版本)-->详见:本文2、使用CXF框架开发-->详见:其他文章二、使用JDK开发WebServi