c++如何使用gsoap调用java的webservice接口,【工作笔记】c++使用gsoap调用webservice

本文介绍了如何使用gSOAP工具在C++中调用第三方的WebService接口,通过一个天气查询的demo程序展示了gSOAP的使用步骤,包括下载gSOAP、编译生成代码、创建调用示例程序以及执行结果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[gSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在Windows、Linux、MAC OS和UNIX下使用C和C++语言编码,集合了SSL功能。下载地址:http://sourceforge.n

工作中需要用c++来调用第三方的webservice接口,于是使用了gsoap工具。为了初步掌握gsoap的使用方法,这里写了一个demo程序来完成对于天气预报查询接口的调用。

安装好gsoap工具,然后使用make gsoap命令编译。

/*makefile文件如下*/[gsoap包下载地址:http://sourceforge.net/projects/gsoap2/files/gSOAP/gSOAP%202.7.16%20stable/gsoap_2.7.16.zip/downloadhttp://sourceforge.net/projects/gsoap2/file

#make file for testing gsoap

all:test

GSOAP_ROOT = /usr/share/gsoap/import

WSDL_FILES = $(wildcard *.wsdl)

OBJS = $(patsubst %.cpp,%.o,$(wildcard *.cpp))

CXX = /usr/bin/g++

CXXFLAGS = -g # -fno-use-linker-plugin

LIBS = -lgsoap++

ifeq ( ,$(WSDL_FILES))

WSDL_FILES = http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

endif

wsdl:

wsdl2h $(WSDL_FILES) -t /usr/share/gsoap/WS/typemap.dat -o weather.h

# -C only client,-I import path,-L no soap*Lib.c file,-x no xml file

gsoap:wsdl

soapcpp2 -iCLx -I $(GSOAP_ROOT) weather.h

test:$(OBJS)

$(CXX) $(CXXFLAGS) $(LIBS) $^ -o $@

clean:

@rm -rf $(OBJS) test

编译完成后会生成 WeatherWebServiceSoap.nsmap、soapWeatherWebServiceSoapProxy等之类的文件。

将这些文件放到你的项目中,然后创建一个调用天气查询的demo程序:test.cpp。

#include "WeatherWebServiceSoap.nsmap"

#include "soapWeatherWebServiceSoapProxy.h"

#include 

#include 

using namespace std;

void showResult(ns1__ArrayOfString* aos){

for (vector::iterator

itr = aos->string.begin(), itr_end = aos->string.end();

itr != itr_end; ++itr)

{

string str = *itr;

const char* str2 = str.c_str();

std::cout <

}

}

int main(int argc, char** argv)

{

struct soap soap;

soap_init(&soap);

//soap_set_imode(&soap, SOAP_C_UTFSTRING);

_ns1__getSupportCity request;

std::string provinceName = "四川";

request.byProvinceName = &provinceName;

_ns1__getSupportCityResponse response;

WeatherWebServiceSoapProxy* serviceProxy = new WeatherWebServiceSoapProxy(SOAP_C_UTFSTRING, SOAP_C_UTFSTRING);

/*获取支持的城市名*/

int result=serviceProxy->getSupportCity(&request,&response);

if (SOAP_OK==result)

{

showResult(response.getSupportCityResult);

}

else

{

std::cout <soap_fault_string() <

}

/*查询城市的天气*/

std::string cityName = "攀枝花";

_ns1__getWeatherbyCityName weatherRequest;

weatherRequest.theCityName = &cityName;

_ns1__getWeatherbyCityNameResponse weatherResponse;

int ret=serviceProxy->getWeatherbyCityName(&weatherRequest, &weatherResponse);

if (SOAP_OK==ret){

showResult(weatherResponse.getWeatherbyCityNameResult);

}

else

{

std::cout <soap_fault_string() <

}

delete serviceProxy;

return 0;

}

使用make 命令编译程序。执行可执行程序,结果如下:

NNnuQn.png[http://blog.youkuaiyun.com/wenzi49312/article/details/8963345预备知识ONVIF规范中设备管理和控制部分所定义的接口均以Web Services的形式提供。ONVIF规范涵盖了完全的XML及W

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值