"Work Notes" C + + calls WebService using gSOAP

Source: Internet
Author: User

In order to use C + + to invoke the third-party WebService interface, the gSOAP tool is used. In order to master the use of gSOAP, a demo program is written to complete the call to the weather forecast query interface.

Install the gSOAP tool and compile with the make gSOAP command.

/*makefile file as follows */

 #make  file for testing gsoapall:testgsoap_root =  /usr/share/gsoap/importwsdl_files = $ (WILDCARD *.WSDL) objs = $ (patsubst %. cpp,%.o,$ (wildcard *.cpp)) cxx = /usr/bin/g++cxxflags = -g # - fno-use-linker-pluginlibs = -lgsoap++ifeq  ( ,$ (wsdl_files)) WSDL_FILES = http:// www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdlendifwsdl: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 filegsoap:wsdlsoapcpp2 -iclx -i $ ( Gsoap_root)  weather.htest:$ (OBJS) $ (CXX)  $ (cxxflags)  $ (LIBS)  $^ -o [email  Protected]clean: @rm  -rf $ (OBJS)  test 

When the compilation is complete, files such as Weatherwebservicesoap.nsmap, Soapweatherwebservicesoapproxy, etc. are generated.

Put these files in your project and create a demo program that calls the weather query: Test.cpp.

#include   "Weatherwebservicesoap.nsmap" #include   "SoapWeatherWebServiceSoapProxy.h" #include  < string> #include  <iostream>using namespace std;void showresult (ns1__ArrayOfString*  aos) {for  (Vector<string>::iteratoritr = aos->string.begin (),  itr_end =  aos->string.end (); itr != itr_end; ++itr) {string str = *itr;const  Char* str2 = str.c_str (); Std::cout << str2 << std::endl;}} Int main (INT&NBSP;ARGC,&NBSP;CHAR**&NBSP;ARGV) {struct soap soap;soap_init (&soap);//soap_set _imode (&soap, soap_c_utfstring); _ns1__getsupportcity request; std::string provincename  =  "Sichuan";request.byprovincename = &provincename;_ns1__getsupportcityresponse  Response Weatherwebservicesoapproxy* serviceproxy = new weatherwebservicesoapproxy (SOAP_C_UTFSTRING ,  soap_c_utfstring);/* Gets the supported city name */int result=serviceproxy->getsupportcity (&request,&response);if  ( Soap_ok==result) {Showresult (response.getsupportcityresult);}  else{std::cout <<  "Error:"  << serviceproxy->soap_fault_string ()   << std::endl << std::endl;} /* Check the weather of the city */std::string cityname =  "Panzhihua";_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 <<  "Error:"  << serviceproxy->soap_fault_string ()  < < std::endl;} delete serviceproxy;return 0;}

Use the make command to compile the program. Executes the executable program with the following results:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7B/69/wKioL1bNYj-i8AE4AABieztxTts825.png "title=" Qq20160224153034.png "alt=" Wkiol1bnyj-i8ae4aabieztxtts825.png "/>

"Work Notes" C + + calls WebService using gSOAP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.