嵌入式Web Service gSOAP的移植与应用
一.实验环境
硬件平台:
ATMEL9260嵌入式平台
PC机Pentium500以上,硬盘40GB以上,内存512MB以上
软件平台:
PC机操作系统RedHat Linux 9.0
gsoap-2.8,下载地截:http://gsoap2.sourceforge.net/
二.实验内容:
1、 安装gsoap
新建一个目录,将gsoap下载到linux服务器上
进入gsoap-2.8文件夹下
yihui@term-uni:~/tools/gsoap-2.8$./configuer
yihui@term-uni:~/tools/gsoap-2.8$make
编译后会在gsoap/bin/linux386目录下生成以下两个执行程序
yihui@term-uni:~/tools/gsoap-2.8/gsoap/bin/linux386$ ls
soapcpp2 wsdl2h
2、从网上下载提供webservice服务的WSDL 文件(http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl)
3、使用 wsdl2h 编译 WSDL 文件生成该服务的头文件,如下图所示:
4、使用soapcpp2 生成 C/C++ 文件,如下图所示:
文件名称 | 描述 |
soapStub.h | 从输入 Header 文件生成的经过修改且带标注的 Header 文件 |
soapH.h | 主 Header 文件,所有客户机和服务源代码都要将其包括在内 |
soapC.c | 指定数据结构的序列化器和反序列化器 |
soapClient.c | 远程操作的客户机存根例程 |
stdsoap2.h | stdsoap2.cpp 运行时库的 Header 文件 |
stdsoap2.cpp | 运行时 C++ 库,带 XML 解析器和运行时支持例程 |
5、根据头文件编写客户端程序asmxclient.c,如下所示:
#include "soapH.h"
#include "WeatherWebServiceSoap.nsmap"
//#include "WeatherWebServiceSoap12.nsmap"
#include <iostream>
#include <vector>
const char server[] = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
int main(int argc, char **argv)
{
struct soap soap;
char city_name[32];
_ns1__getSupportCityResponse result;
_ns1__getSupportCity SupportCity;
memset( city_name, 0, sizeof(city_name) );
if (argc < 3)
{
fprintf(stderr, "Usage: [city] CityName\n");
exit(0);
}
soap_init(&soap);
soap_set_mode(&soap,SOAP_C_UTFSTRING);
strcpy( city_name, argv[2] );
std::string s=(city_name);
SupportCity.byProvinceName = &s;
switch (*argv[1])
{
case 'c':
std::cout << *SupportCity.byProvinceName << std::endl;
soap_call___ns2__getSupportCity(&soap, server, "", &SupportCity, &result);
break;
default:
fprintf(stderr, "Unknown command\n");
exit(0);
}
if (soap.error)
{
soap_print_fault(&soap, stderr);
exit(1);
}
else
{
printf("string.size=%d\n", result.getSupportCityResult->string.size() );
std::vector<std::string>::iterator p;
for (p = result.getSupportCityResult->string.begin(); p != result.getSupportCityResult->string.end(); p++)
{
std::cout << *p << std::endl;
}
}
soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap);
return 0;
}
6、编辑Makefile文件(stdsoap2.h,stdsoap2.c,stdsoap2.cpp以及improt目录都是sgoap自带的,请拷到相应的目录下),Makefile文件内容如下:
GSOAP=../bin/soapcpp2
SOAPH=../include/stdsoap2.h
SOAPC=../pub/stdsoap2.c
SOAPCPP=../pub/stdsoap2.cpp
CC=arm-linux-gcc
CPP=g++
LIBS=
COFLAGS=-O2
CWFLAGS=-Wall
CIFLAGS=-I../include -I../import
CMFLAGS=
CFLAGS= $(CWFLAGS) $(COFLAGS) $(CIFLAGS) $(CMFLAGS)
all: asmxclient
asmxclient: WeatherWebService.h asmxclient.c $(SOAPH) $(SOAPC)
$(GSOAP) WeatherWebService.h $(CIFLAGS)
$(CPP) $(CFLAGS) -o asmxclient asmxclient.c soapC.cpp soapClient.cpp $(SOAPC) $(LIBS)
clean:
rm -f *.o soapH.h soapStub.h soapC.cpp soapC.c soapClient.cpp soapClient.c soapServer.cpp soapServer.c soap*Proxy.h
distclean:
rm -f *.o *.xsd *.xml *.nsmap *.log asmxclient soap*
7、运行程序,如下图所示:
8、将程序移植到arm平台上,只需将Makefile文件中的CPP=g++
改为arm-linux-g++ 即可