导航:首页 > 编程语言 > java调用gsoap

java调用gsoap

发布时间:2022-04-25 06:22:45

❶ 怎么使用gsoap

接下来我结合自己的实践与理解,讲讲VC用gsoap下编写webService和客户端程序,有不对的地方还请大家指正,谢谢。
我以网上出现的实现一个简单的加法函数为例,讲讲我在操作过程中遇到的问题。


服务器端
1.首先编写 add.h文件:

1//gsoap ns service name: add
2//gsoap ns service namespace: http://localhost/add.wsdl
3//gsoap ns service location: http://localhost
4//gsoap ns service executable: add.cgi
5//gsoap ns service encoding: encoded
6//gsoap ns schema namespace: urn:add
7
8int ns__add( int num1, int num2, int* sum );
9

2.用gsoap/bin目录下的soapcpp2.exe程序,生成一些文件。可以把soapcpp2.exe拷贝到一add.h目录下,用cmd执行soapcpp2.exe
add.h就可以,在这个目录下会自动生成许多将来有用的文件,如add.namap,soapH.h,soapC.cpp,soapClient.cpp,soapServer.cpp等文件。soapcpp2.exe可以带参数执行,具体执行soapcpp2.exe
-h查看。

3.新建一个win32控制台工程,加入wsock32.lib库,将刚才生成的那些文件添加到工程中。然后编写webserver.cpp主程序:

#include "add.h"
#include "add.nsmap"

int main(int argc, char* argv[])
{

int m, s; /**//* master and slave sockets */
struct soap add_soap;
soap_init(&add_soap);
//soap_set_namespaces(&add_soap, add_namespaces);

if (argc < 2)
{
printf("usage: %s <server_port> \n", argv[0]);
exit(1);
}
else
{
m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
if (m < 0)
{
soap_print_fault(&add_soap, stderr);
exit(-1);
}

fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for ( ; ; )
{
s = soap_accept(&add_soap);
if (s < 0)
{
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);

soap_serve(&add_soap);//该句说明该server的服务
soap_end(&add_soap);
}
}
return 0;
}
//server端的实现函数与add.h中声明的函数相同,但是多了一个当前的soap连接的参数
int ns__add(struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return 0;
}

4.
编译这个程序,会提示错误,将gsoap_win32目录下stdsoap2.cpp,stdsoap2.h文件加入工程,重新编译如果还有错误,可能是你将add.h生成的文件添加入工程出错的原因。实际上在编写server程序时,无须带Client的那些文件,还有带Lib的文件也无须添加到工程中。再重新编译应该就没有问题了,启动4567端口,在ie中输入localhost:4567,如果显示xml页面,说明程序已经启动。


对应的客户端
1。客户端程序代码如下:

#include <stdio.h>
#include <stdlib.h>
#include "soapH.h"
#include "add.nsmap"

int add(const char* server, int num1, int num2, int *sum);

int main(int argc, char **argv)
{
int result = -1;
char* server="http://localhost:4567";
int num1 = 0;
int num2 = 0;
int sum = 0;
if( argc < 3 )
{
printf("usage: %s num1 num2 \n", argv[0]);
exit(0);
}

num1 = atoi(argv[1]);
num2 = atoi(argv[2]);

result = add(server, num1, num2, &sum);
if (result != 0)
{
printf("soap err,errcode = %d\n", result);
}
else
{
printf("%d+%d=%d\n", num1, num2, sum );
}
return 0;
}

int add( const char* server, int num1, int num2, int *sum )
{
struct soap add_soap;
int result = 0;
soap_init(&add_soap);
// soap_set_namespaces(&add_soap, add_namespaces);

//该函数是客户端调用的主要函数,后面几个参数和add.h中声明的一样,前面多了3个参数,函数名是接口函数名ns__add前面加上soap_call_
soap_call_ns__add( &add_soap, server, "", num1, num2, sum );
if(add_soap.error)
{
printf("soap error:%d,%s,%s\n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap) );
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
return result;
}

2.客户端程序既可以新建一个新的win32控制台程序,将刚才生成的nsmap,soapH.h,soapClient.h等文件加入工程,编译既可。我是直接在原先工程中加入一客户端代码,将webserver.cpp文件移除,并且将soapServer.cpp等server端需要的文件移除,将soapClient.cpp等client端需要的cpp添加到工程,编译既可。
3.启动server程序,F5客户端程序,经测试正常。


遇到的问题
1.server端可以编译成CGI方式执行,而并不是绑定到某个端口,这种方式我没有实践。

if (argc < 2) // no args: assume this is a CGI application
{
soap_serve(&soap); // serve request, one thread, CGI style
soap_destroy(&soap); // dealloc C++ data
soap_end(&soap); // dealloc data and clean up
}
2.在编译服务器及客户端程序时一开始对add.h生成的文件添加到工程,经常出现问题,需要自己不调试。特别是链接时段,server/client要与其生成的文件相对应,server调用生成的soapserver.cpp,client调用生成的soapclient.cpp文件。
3.多线程方式,在windows下建议用pthread_win32库,这里给出多线程下的例子。

一 gSOAP需要的头文件:

//gsoap ns service name: calc
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: http://127.0.0.1:8089/calc.wsdl
//gsoap ns service location: http://127.0.0.1:8089/cal
//gsoap ns schema namespace: urn:calc
int ns__add(double a, double b, double *result);
int ns__sub(double a, double b, double *result);
int ns__mul(double a, double b, double *result);
int ns__div(double a, double b, double *result);
int ns__pow(double a, double b, double *result);

二 多线程服务器关键代码

#include
#include "calc.nsmap"
#include "soapH.h"

/**//////////////////////////////////////////////////////////////////////////
///宏与全局变量的定义
#define BACKLOG (100)
#define MAX_THR (10)
#define MAX_QUEUE (1000)

pthread_mutex_t queue_cs; //队列锁
pthread_cond_t queue_cv; //条件变量
SOAP_SOCKET queue[MAX_QUEUE]; //数组队列
int head =0, tail =0; //队列头队列尾初始化
/**///////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
void * process_queue(void *); //线程入口函数
int enqueue(SOAP_SOCKET); //入队列函数
SOAP_SOCKET dequeue(void); //出队列函数

/**///////////////////////////////////////////////////////////////////////////
//线程入口函数
void * process_queue(void * soap)
{
struct soap * tsoap = (struct soap *)soap;
for(;;)
{
tsoap->socket = dequeue();
if (!soap_valid_socket(tsoap->socket))
{
break;
}
soap_serve(tsoap);
soap_destroy(tsoap);
soap_end(tsoap);
}
return NULL;
}

//入队列操作
int enqueue(SOAP_SOCKET sock)
{
int status = SOAP_OK;
int next;
pthread_mutex_lock(&queue_cs);
next = tail +1;
if (next >= MAX_QUEUE)
next = 0;
if (next == head)
status = SOAP_EOM;
else
{
queue[tail] =sock;
tail = next;
}
pthread_cond_signal(&queue_cv);
pthread_mutex_unlock(&queue_cs);
return status;
}

//出队列操作
SOAP_SOCKET dequeue()
{
SOAP_SOCKET sock;
pthread_mutex_lock(&queue_cs);
while (head == tail )
{
pthread_cond_wait(&queue_cv,&queue_cs);
}
sock = queue[head++];
if (head >= MAX_QUEUE)
{
head =0;
}
pthread_mutex_unlock(&queue_cs);
return sock;
}

/**///////////////////////////具体服务方法////////////////////////////////////////
//加法的实现
int ns__add(struct soap *soap, double a, double b, double *result)
{
*result = a + b;
return SOAP_OK;
}
//减法的实现
int ns__sub(struct soap *soap, double a, double b, double *result)
{
*result = a - b;
return SOAP_OK;
}
//乘法的实现
int ns__mul(struct soap *soap, double a, double b, double *result)
{
*result = a * b;
return SOAP_OK;
}
//除法的实现
int ns__div(struct soap *soap, double a, double b, double *result)
{
if (b)
*result = a / b;
else
{
char *s = (char*)soap_malloc(soap, 1024);
sprintf(s, "Can't">http://tempuri.org/">Can't divide %f by %f", a, b);
return soap_sender_fault(soap, "Division by zero", s);
}
return SOAP_OK;
}
//乘方的实现
int ns__pow(struct soap *soap, double a, double b, double *result)
{
*result = pow(a, b);
if (soap_errno == EDOM) /**//* soap_errno 和errorno类似, 但是和widnows兼容 */
{
char *s = (char*)soap_malloc(soap, 1024);
sprintf(s, "Can't take the power of %f to %f", a, b);
sprintf(s, "Can't">http://tempuri.org/">Can't take power of %f to %f", a, b);
return soap_sender_fault(soap, "Power function domain error", s);
}
return SOAP_OK;
}

/**///////////////////////////////////////////////////////////////////////////////////////////////////////
//主函数
int main(int argc,char ** argv)
{
struct soap ServerSoap;
//初始话运行时环境
soap_init(&ServerSoap);
//如果没有参数,当作CGI程序处理
if (argc <2)
{
//CGI 风格服务请求,单线程
soap_serve(&ServerSoap);
//清除序列化的类的实例
soap_destroy(&ServerSoap);
//清除序列化的数据
soap_end(&ServerSoap);
}else
{
struct soap * soap_thr[MAX_THR];
pthread_t tid[MAX_THR];
int i,port = atoi(argv[1]);
SOAP_SOCKET m,s;
//锁和条件变量初始化
pthread_mutex_init(&queue_cs,NULL);
pthread_cond_init(&queue_cv,NULL);
//绑定服务端口
m = soap_bind(&ServerSoap,NULL,port,BACKLOG);
//循环直至服务套接字合法
while (!soap_valid_socket(m))
{
fprintf(stderr,"Bind port error! ");
m = soap_bind(&ServerSoap,NULL,port,BACKLOG);
}
fprintf(stderr,"socket connection successful %d ",m);

//生成服务线程
for(i = 0; i <MAX_THR; i++)

{
soap_thr[i] = soap_(&ServerSoap);
fprintf(stderr,"Starting thread %d ",i);
pthread_create(&tid[i],NULL,(void*(*)(void*))process_queue,(void*)soap_thr[i]);
}

for(;;)
{
//接受客户端的连接
s = soap_accept(&ServerSoap);
if (!soap_valid_socket(s))
{
if (ServerSoap.errnum)
{
soap_print_fault(&ServerSoap,stderr);
continue;
}else
{
fprintf(stderr,"Server timed out ");
break;
}
}
//客户端的IP地址
fprintf(stderr,"Accepted connection from IP= %d.%d.%d.%d socket = %d ",
((ServerSoap.ip)>>24)&&0xFF,((ServerSoap.ip)>>16)&0xFF,((ServerSoap.ip)>>8)&0xFF,(ServerSoap.ip)&0xFF,(ServerSoap.socket));
//请求的套接字进入队列,如果队列已满则循环等待
while(enqueue(s) == SOAP_EOM)
Sleep(1000);
}
//服务结束后的清理工作
for(i = 0; i < MAX_THR; i++)
{
while (enqueue(SOAP_INVALID_SOCKET) == SOAP_EOM)
{
Sleep(1000);
}
}
for(i=0; i< MAX_THR; i++)
{
fprintf(stderr,"Waiting for thread %d to terminate ..",i);
pthread_join(tid[i],NULL);
fprintf(stderr,"terminated ");
soap_done(soap_thr[i]);
free(soap_thr[i]);
}
pthread_mutex_destroy(&queue_cs);
pthread_cond_destroy(&queue_cv);
}
//分离运行时的环境
soap_done(&ServerSoap);
return 0;
}

❷ 调用ws(webservice)接口时可以不用gsoap么还有什么实现方式

1、C++可以实现webservice,这是毋庸置疑的.axis2本质是运行在tomcat下的一个servlet,分java版本,和C语言版本.官方网站为:,首页上写着:
The well known Apache Axis, and the the second generation of it, the Apache Axis2, are two Web Service containers that helps users to create, deploy, and run Web Services.Axis2 is avaialble in both Java as well as C, languages and details about each version can be found below. 大概意思就是这东西分java版本和C版本,可以方便用户创建,部署,运行web service.而C++完全是兼容C的.
2、需要服务器,要实现某个服务吧,至于怎样为其他平台服务,主要是监听端口实现解析http协议.js不需要拼串成XML,服务器才要拼串,JS是运行在客户端的,客户端也不是通过SOAP与服务端进行通讯的,而是根据需要调用的服务的WSDL,提供对应参数,客户端与服务端的通讯是用http协议的,而通讯方式根据是GET还是POST把相关参数放到HTTP头或者体中.而web service之间的通讯才可能用得到SOAP.
3、PHP调用web service是非常简单的,貌似有个函数通过SOAP调用.C++编写的web service肯定有WSDL,可以根据WSDL描述的端口参数,来调用.
数值和的平均值。 特别提醒:如果引

❸ 如何在C/C++中调用Java

Java端可以做成网络服务,方法就很多了,可以是RESTful形式、基于SOAP的WebService、或者用Netty等。C/C++端可以通过开源库libcurl调用RESTful形式的接口、可以通过gSoap调用基于SOAP的WebService接口。

❹ java调用webservice怎么添加 SoapHeader 做验证

如果你是用axis生成的代码的话 去用CUX_0_WS_SERVER_PRG_BindingStub.java 这个类调用服务。
CUX_0_WS_SERVER_PRG_Service service=new CUX_0_WS_SERVER_PRG_ServiceLocator();
CUX_0_WS_SERVER_PRG_BindingStub stub=(CUX_0_WS_SERVER_PRG_BindingStub)service.CUX_0_WS_SERVER_PRGSOAP();
stub.setUsername("aaaa"); //连接的用户名
stub.setPassword("aaaa"); //连接的密码
stub.setHeader( dddd); //dddd是一个SOAPHeader 具体看对方wsdl要求
stub.invokefmsws(so.in);

不知道对方要求怎么验证 是在header中添加用户名和密码 还是在连接时需提供用户名密码。
所以把添加header 和 使用用户名 密码连接 验证都写了。 找对方确认下用那种方式然后自己试一下。

❺ JAVA,不用TOMCAT等WEB服务器如果实现SOAP服务

目前我做的服务端,都是用web project做的,所以必须跑在tomcat等容器上。你想用一个框架,用java project来实现服务端吗?好像是不行——如果想实现客户端,用java project倒是没有问题的

❻ gSOAP基于 HTTP 的基本认证 (Basic Authentication)

od

❼ gsoap怎么调用webservice 接口函数

1、下载soap

2、解压到c:\gsoap-2.7

3、将c:\gsoap-2.7下的soapcpp2.exe,wsdl2h.exe,stdsoap2.h,stdsoap2.cpp拷贝到C:\wstest\client\gsoap2.7目录下

4、在C:\wstest\client\gsoap2.7目录下执行soapcpp2 -C -x test.h -I "C:/gsoap-2.7/gsoap/import"

5、在C:\wstest\client\gsoap2.7目录下执行wsdl2h -I "C:/gsoap-2.7/gsoap/WS" -s
-o test.h http://192.168.81.191:5000/....../CWSCrm.asmx?wsdl

6、将C:\wstest\client\gsoap2.7目录下文件拷贝到C:\wstest\client\test\目录下

7、新建立qt5项目,test.pro文件:
QT += core gui
QT +=network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
SOURCES += main.cpp\

mainwindow.cpp \

stdsoap2.cpp \

soapC.cpp \
soapClient.cpp
HEADERS += mainwindow.h \

stdsoap2.h \

soapH.h \

stdsoap2.h \

soapStub.h
FORMS += mainwindow.ui
LIBS += -L . -l ws2_32

mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include "CWSCrmSoap.nsmap"
#include "soapCWSCrmSoapProxy.h"
#include "soapH.h"

wsdl2h常用选项
-o 文件名,指定输出头文件
-n 名空间前缀 代替默认的ns
-c 产生纯C代码,否则是C++代码
-s 不要使用STL代码
-t 文件名,指定type map文件,默认为typemap.dat
-e 禁止为enum成员加上名空间前缀

soapcpp2常用选项
-C 仅生成客户端代码
-S 仅生成服务器端代码
-L 不要产生soapClientLib.c和soapServerLib.c文件
-c 产生纯C代码,否则是C++代码(与头文件有关)
-I 指定import路径
-x 不要产生XML示例文件
-i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。

❽ 我手上现在做一个指纹识别的二次开发,开发包中没有Java的例子,遇到以下问题

想java调dll的话 只有2种方法,可能稍微绝对,反正我知道的 只有这2种,第一就是jna或者jni了 两者原理一样,只不过一个是封装了而已,这个肯定能行,多看例子吧,第二种就是借助webservice了,对外提供一个接口,这个接口就类似于地址,java访问到地址自然就能得到一个json串,这个json串就是dll生成的,如果采用第二种方法建议你使用gsoap,能节省很多代码量,但是前提你要对c++比较熟悉。就讲这么多吧,jni跟webservice的例子好多的,一搜一把的。

❾ gsoap 调用webservice内存泄漏

gsoap调用webservice出现内存泄漏
程序运行起来后内存一直增长,出现了内存泄漏,经过各模块的测试分析,将泄漏代码出现在这一段。
/*webservice客户端函数,上传本地数据库数据到远程服务器*/
int
sendtowebservice(char
**data_values,int
n_columns,sqlite3*
conn)
{
char
sql[200]="
";
char
*err_msg;
int
res;
struct
soap
*clientsoap
=
soap_new();
soap_cmac
_ns1__sendonemessage
sendmsg;
soap_cmac
_ns1__sendonemessageresponse
sendmsgresponse;
#if
1
soap_init(clientsoap);
sendmsg.grpid=atoi(data_values[0]);
sendmsg.ctime=atoi(data_values[1]);
sendmsg.allencount=atoi(data_values[2]);
sendmsg.alloutcount=atoi(data_values[3]);
printf("sendmsg.grpid=%d;sendmsg.ctime=%d;sendmsg.encount=%d;sendmsg.outcount=%d\n",sendmsg.grpid,sendmsg.ctime,sendmsg.allencount,sendmsg.alloutcount);
sprintf_s(buffer,sizeof(buffer),"sendmsg.grpid=%d;sendmsg.ctime=%d;sendmsg.encount=%d;sendmsg.outcount=%d",sendmsg.grpid,sendmsg.ctime,sendmsg.allencount,sendmsg.alloutcount);
writelog(buffer);
struct
soap_env__header
header;
clientsoap->header=&header;
string
strid("admin");
string
strpsw("123456");
soap_cmac
ns1__mysoapheader
mysoapheader;
mysoapheader.userid=&strid;
mysoapheader.userpw=&strpsw;
header.ns1__mysoapheader_=&mysoapheader;
clientsoap->header=&header;
if(soap_call___ns1__sendonemessage(clientsoap,
null,
null,
&sendmsg,
&sendmsgresponse)==soap_ok)
{
//printf("response=%d\n
",
sendmsgresponse.sendonemessageresult);
sprintf_s(buffer,sizeof(buffer),"sendmsgresponse.sendonemessageresult=%d",sendmsgresponse.sendonemessageresult);
writelog(buffer);
/*
-1
=验证失败,
-2=失败,1=插入成功,2=更新成功;
插入成功,更新成功写数据库,将数据标为已发送。
失败,返回-1,上传数据线程释放资源
*/
switch(sendmsgresponse.sendonemessageresult)
{
case
0:
soap_destroy(clientsoap);
soap_end(clientsoap);
soap_done(clientsoap);
return
-1;
break;
case
1:
writelog("上传到服务器,数据插入成功");
sprintf_s(sql,sizeof(sql),"update
grp
set
issend=%d
where
ctime=%d
and
gropid=%d",1,atoi(data_values[1]),atoi(data_values[0]));
res=sqlite3_exec(conn,
sql,
null,
0,
&err_msg);
if(res!=sqlite_ok)
{
fprintf(stderr,"操作失败,错误代码:%s",err_msg);
sprintf_s(buffer,sizeof(buffer),"操作失败,错误代码:%s",err_msg);
writelog(buffer);
}
else
{
printf("本地数据issend更新为1成功\n");
writelog("本地数据issend更新为1成功");
}
sqlite3_free(err_msg);
break;
case
2:
writelog("上传到服务器,数据更新成功");
sprintf_s(sql,sizeof(sql),"update
grp
set
issend=%d
where
ctime=%d
and
gropid=%d",1,atoi(data_values[1]),atoi(data_values[0]));
res=sqlite3_exec(conn,
sql,
null,
0,
&err_msg);
if(res!=sqlite_ok)
{
fprintf(stderr,"操作失败,错误代码:%s\n",err_msg);
sprintf_s(buffer,sizeof(buffer),"操作失败,错误代码:%s",err_msg);
writelog(buffer);
}
else
{
printf("本地数据issend更新为1成功\n");
writelog("本地数据issend更新为1成功");
}
sqlite3_free(err_msg);
break;
case
-1:
writelog("-1,连接服务器验证失败");
soap_destroy(clientsoap);
soap_end(clientsoap);
soap_done(clientsoap);
return
-1;
break;
case
-2:
writelog("-2,失败");
soap_destroy(clientsoap);
soap_end(clientsoap);
soap_done(clientsoap);

❿ fastcgi怎么调用gsoap代码

1、下载soap 2、解压到c:\gsoap-2.7 3、将c:\gsoap-2.7下的soapcpp2.exe,wsdl2h.exe,stdsoap2.h,stdsoap2.cpp拷贝到C:\wstest\client\gsoap2.7目录下 4、在C:\wstest\client\gsoap2.7目录下执行soapcpp2 -C -x test.h -I "C:/gsoap-2.7/gsoap/i...

阅读全文

与java调用gsoap相关的资料

热点内容
二手开利螺杆压缩机 浏览:309
有php基础学java要多久 浏览:300
程序员税后工资多少可以跳槽 浏览:172
个别网站无法解析服务器的dns地址 浏览:972
安卓手机如何打开rmb文件 浏览:215
新生儿app叫什么 浏览:65
斗鱼加密怎么弄 浏览:761
为什么会加密不可上网 浏览:531
步步高手机编译时间啥意思 浏览:396
程序员复盘app 浏览:160
pdf确定 浏览:536
php连接mysql端口号 浏览:999
id3算法在进行某个节点划分时 浏览:406
麦块服务器如何登录正版 浏览:687
中国民俗学pdf 浏览:387
程序员如何做人力资源 浏览:658
p单片机数字电压表项目设计报告 浏览:450
做一个单片机系统要经过哪些步骤 浏览:153
阿里云php版本升级 浏览:355
pdf转换word绿色 浏览:359