Linux下C gsoap client访问java webservice server
程序员文章站
2022-05-25 23:37:06
...
1 http://gsoap2.sourceforge.net/ 下载gsoap_2.7.15.tar.gz
2 tar -zxvf gsoap_2.7.15.tar.gz
3 ./configure(默认安装在/usr/local/share/gsoap目录下,也可以使用 ./configure –-prefix=/usr/local/gsoap指定安装路径)
4 make
5 su输入密码转到root用户
6 make install
7 创建工程目录,mkdir calculator
8 进入工程目录cd calculator
9 cp /home/jimmy/development/webservice/gsoap-2.7/gsoap/stdsoap2.c .
cp /home/jimmy/development/webservice/gsoap-2.7/gsoap/stdsoap2.h .
10 mkdir include
11 cp /home/jimmy/development/webservice/gsoap-2.7/gsoap/libgsoap.a ./include
12 /usr/local/bin/wsdl2h -c -o calculator.h http://192.168.0.125:8888/IntelligenceAppliance/CalculatorPort?wsdl
** The gSOAP WSDL/Schema processor for C and C++, wsdl2h release 1.2.15
** Copyright (C) 2000-2009 Robert van Engelen, Genivia Inc.
** All Rights Reserved. This product is provided "as is", without any warranty.
** The wsdl2h tool is released under one of the following two licenses:
** GPL or the commercial license by Genivia Inc. Use option -l for more info.
Saving calculator.h
Cannot open file 'typemap.dat'
Problem reading type map file 'typemap.dat'.
Using internal type definitions for C instead.
Connecting to 'http://192.168.0.125:8888/IntelligenceAppliance/CalculatorPort?wsdl' to retrieve WSDL/XSD... connected, receiving...
Connecting to 'http://192.168.0.125:8888/IntelligenceAppliance/CalculatorPort?xsd=1' to retrieve schema... connected, receiving... done reading 'http://192.168.0.125:8888/IntelligenceAppliance/CalculatorPort?xsd=1'
done reading 'http://192.168.0.125:8888/IntelligenceAppliance/CalculatorPort?wsdl'
To complete the process, compile with:
soapcpp2 calculator.h
[jimmy@srp calculator]$
13 /usr/local/bin/soapcpp2 -c -C calculator.h
** The gSOAP code generator for C and C++, soapcpp2 release 2.7.15
** Copyright (C) 2000-2009, Robert van Engelen, Genivia Inc.
** All Rights Reserved. This product is provided "as is", without any warranty.
** The soapcpp2 tool is released under one of the following three licenses:
** GPL, the gSOAP public license, or the commercial license by Genivia Inc.
Saving soapStub.h annotated copy of the input definitions
Saving soapH.h serializers
Saving soapC.c serializers
Saving soapClient.c client calling stubs
Saving soapClientLib.c client stubs with static serializers for libs
Using ns1 service name: CalculatorPortBinding
Using ns1 service style: document
Using ns1 service encoding: literal
Using ns1 service location: http://192.168.0.125:8888/IntelligenceAppliance/CalculatorPort
Using ns1 schema namespace: http://webservice.nk.cn/
Saving CalculatorPortBinding.substract.req.xml sample SOAP/XML request
Saving CalculatorPortBinding.substract.res.xml sample SOAP/XML response
Saving CalculatorPortBinding.add.req.xml sample SOAP/XML request
Saving CalculatorPortBinding.add.res.xml sample SOAP/XML response
Saving CalculatorPortBinding.divide.req.xml sample SOAP/XML request
Saving CalculatorPortBinding.divide.res.xml sample SOAP/XML response
Saving CalculatorPortBinding.multiply.req.xml sample SOAP/XML request
Saving CalculatorPortBinding.multiply.res.xml sample SOAP/XML response
Saving CalculatorPortBinding.nsmap namespace mapping table
Compilation successful
[jimmy@srp calculator]$
14 创建calculator.c,内容如下:
1 #include <stdio.h>
2 #include "soapH.h"
3 #include "CalculatorPortBinding.nsmap"
4
5 int main(int argc, char *argv[])
6 {
7 struct soap *soap = soap_new();
8 struct ns1__add add;
9 struct ns1__substract sub;
10 struct ns1__multiply mul;
11 struct ns1__divide div;
12 struct ns1__addResponse add_ret;
13 struct ns1__substractResponse sub_ret;
14 struct ns1__multiplyResponse mul_ret;
15 struct ns1__divideResponse div_ret;
16
17 int result;
18
19 add.arg0 = 1;
20 add.arg1 = 2;
21
22 sub.arg0 = 1;
23 sub.arg1 = 2;
24
25 mul.arg0 = 1;
26 mul.arg1 = 2;
27
28 div.arg0 = 8;
29 div.arg1 = 2;
30
31
32 if(soap_call___ns1__add(soap, NULL, NULL, &add, &add_ret) == SOAP_OK)
33 printf("The sum of 1 and 2 is %d\n", add_ret.return_);
34 else
35 soap_print_fault(soap, stderr);
36
37 if(soap_call___ns1__substract(soap, NULL, NULL, &sub, &sub_ret) == SOAP_OK)
38 printf("The substract of 1 and 2 is %d\n", sub_ret.return_);
39 else
40 soap_print_fault(soap, stderr);
41
42 if(soap_call___ns1__multiply(soap, NULL, NULL, &mul, &mul_ret) == SOAP_OK)
43 printf("The multiply of 1 and 2 is %d\n", mul_ret.return_);
44 else
45 soap_print_fault(soap, stderr);
46
47 if(soap_call___ns1__divide(soap, NULL, NULL, &div, &div_ret) == SOAP_OK)
48 printf("The divide of 8 and 2 is %d\n", div_ret.return_);
49 else
50 soap_print_fault(soap, stderr);
51
52 soap_end(soap);
53 soap_free(soap);
54 }
15 创建Makefile
#INCLUDE=$(GSOAP_ROOT)
INCLUDE=./include
OBJS=soapC.o stdsoap2.o soapClient.o calculator.o
calculator: $(OBJS)
gcc -I $(INCLUDE) -o calculator $(OBJS)
.PHONY: clean
clean:
rm *.o calculator
16 make
cc -c -o soapC.o soapC.c
cc -c -o stdsoap2.o stdsoap2.c
cc -c -o soapClient.o soapClient.c
cc -c -o calculator.o calculator.c
gcc -I ./include -o calculator soapC.o stdsoap2.o soapClient.o calculator.o
17 ./calculator
The sum of 1 and 2 is 3
The substract of 1 and 2 is -1
The multiply of 1 and 2 is 2
The divide of 8 and 2 is 4
18 目录结构
calculator
calculator.c
calculator.h
CalculatorPortBinding.add.req.xml
CalculatorPortBinding.add.res.xml
CalculatorPortBinding.divide.req.xml
CalculatorPortBinding.divide.res.xml
CalculatorPortBinding.multiply.req.xml
CalculatorPortBinding.multiply.res.xml
CalculatorPortBinding.nsmap
CalculatorPortBinding.substract.req.xml
CalculatorPortBinding.substract.res.xml
include
libgsoap.a
Makefile
soapC.c
soapClient.c
soapClientLib.c
soapH.h
soapStub.h
stdsoap2.c
stdsoap2.h
19 总结
把所有的xml文件删除依然可以正确编译运行
soapStub.h中包含所有的在calculator.c中使用的结构体和函数声明
soapcpp2常用选项
* -C 仅生成客户端代码
* -S 仅生成服务器端代码
* -L 不要产生soapClientLib.c和soapServerLib.c文件
* -c 产生纯C代码,否则是C++代码(与头文件有关)
* -I 指定import路径(见上文)
* -x 不要产生XML示例文件
* -i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。
/usr/local/bin/soapcpp2 -c calculator.h则产生服务器端和客户端的代码
20、安装注意:
在README.txt文件中:
To build without OpenSSL support, use:
$ ./configure --disable-openssl
$ make
$ make install
我在ubuntu下安装采用此方法