一、WebService例子
1.准備要調用的webservice接口的wsdl地址,比如網上的查詢天氣接口:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
2.准備gSOAP工具:將gsoap_2.8.100.zip解壓,進入gsoap_2.8.100\gsoap-2.8\gsoap\bin\win32目錄下(工具可以在這個網址下載:https://sourceforge.net/projects/gsoap2/files/gsoap-2.8/)
3.將wsdl文件保存到win32目錄下,如下;
4.在該目錄下打開cmd窗口(按住shift鍵,然后點擊鼠標右鍵,選擇“在此處打開命令窗口”),如下:
5.在黑窗口中輸入命令:wsdl2h -s WeatherWS.wsdl,點擊回車生成WeatherWS.h頭文件
6.繼續在黑窗口輸入命令:soapcpp2 -i -C -x -L WeatherWS.h -IE:\CPLUSPLUSEX\gsoap_2.8.100\gsoap-2.8\gsoap\import,點擊回車生成一些頭文件和源文件;(注意:我的gSOAP放在的是E:\CPLUSPLUSEX\下面,你們自己根據自己的路徑輸入)
7.打開Visual Studio 2012,在里面新建一個空項目
8.將文件復制到“頭文件”和“源文件”里面,如下所示(注意:應該先把所有文件都要復制到項目的文件夾里面,再從項目文件復制到“頭文件”和“源文件”里面,其中的stdsoap2.h和stdsoap2.cpp在gsoap_2.8.100\gsoap-2.8\gsoap目錄下。反正我是這樣搞的,不然報些錯誤)
9.編寫調用接口的代碼,代碼如下:
#include <stdio.h> #include <stdlib.h> #include <fstream> //包含soap頭文件 #include "soapH.h" #include "soapStub.h" #include "WeatherWSSoap.nsmap" #include "soapWeatherWSSoapProxy.h" using namespace std; int main(int argc, char **argv) { //WebService的請求地址 char* web_url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx"; //soap接口 WeatherWSSoapProxy soap(SOAP_C_UTFSTRING); //構造輸入參數 _ns1__getWeather city_name; city_name.theCityCode = ""; city_name.theUserID = ""; //輸出參數 _ns1__getWeatherResponse weather_res; //調用接口方法getWeather int xlt = soap.getWeather(web_url, NULL, &city_name, weather_res); //判斷接口返回值, SOAPOK表示成功 if (xlt == SOAP_OK) { // 獲取返回結果 ns1__ArrayOfString* aos = weather_res.getWeatherResult; // 打印返回結果 int count = aos -> __sizestring; char **result = aos -> string; for (int i = 0; i < count; i++) { cout << result[i] << endl; } } getchar(); return 0; }
10.跑起來,發現有結果,但有亂碼。其實我入參傳的空值,默認返回的應該是上海的天氣。亂碼是因為編碼不是使用UTF-8。
二、WebService第二個demo
手頭有個項目需要調用webservice接口,選取的開發語言為c++,下面是我的預研結果:
1、C++調用webservice接口環境准備
使用gsoap工具類庫,你可以通過鏈接去下載最新版本,按照步驟去安裝(省去......無非是./configure&&make&&makeinstall)
備注:本人使用的gsoap版本為 gsoap-2.8
2、webservice接口解析獲得C++文件
下面這個命令是將webservice接口文檔解析到outfile.h文件中,infile.wsdl是你下載的接口文件或者直接將WSDL的URL替換也可以,后面的實例會用到;
wsdl2h -o outfile.h infile.wsdl
or
wsdl2h -o outfile.h http://www.xmethods.net/wsdl/query.wsdl
接下來生成C++接口文件:
soapcpp2 -j outfile.h
若你要生成純C的接口文件,使用參數 -c:
soapcpp2 -c outfile.h
這一步有時會出現找不到頭文件,只需要執行時添加-I參數即可,如:
soapcpp2 -j outfile.h -I/home/***/.../import
走到這一步就完成了准備工作,接下來就可以開始編程了
3、實例
webservice接口地址:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
此為國內手機號碼歸屬地查詢的一個接口,在網上找的,不可盡信,因為接口需要userid,我只是走個過場,SOAP正常即達到目的
第一步生成頭文件:
wsdl2h -o mobile.h http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
此時你的目錄中會生成mobile.h的頭文件
第二步生成對應的接口類文件:
soapcpp2 -j mobile.h -I/home/demon/gsoap-2.8/gsoap/import
你可以不加-I,除非編譯器找不到頭文件
第三步開始編程,我只寫了個測試的主函數:
-
-
-
using namespace std;
-
-
int main()
-
{
-
MobileCodeWSSoapProxy proxy;
-
_ns1__getMobileCodeInfo *info = new _ns1__getMobileCodeInfo();
-
info->mobileCode = new string("13488889999");
-
info->userID = new string("888");
-
_ns1__getMobileCodeInfoResponse *response = new _ns1__getMobileCodeInfoResponse();
-
int ret = proxy.getMobileCodeInfo(info, response);
-
cout<<"SOAP_OK:"<<SOAP_OK<<";RET:"<<ret<<";RETMSG:"<<*(response->getMobileCodeInfoResult)<<endl;
-
return 0;
-
}
第四步編寫簡易makefile:
OBJS=test.o soapC.o soapMobileCodeWSSoapProxy.o
EXE=test
CPPFLAGS=-g -Wall -std=c++11 -I/home/demon/gsoap-2.8/gsoap -L/home/demon/gsoap-2.8/gsoap -lgsoap++
$(EXE):$(OBJS) g++ $^ -o $@ $(CPPFLAGS) clean: rm *.o
編譯通過之后執行結果為:
SOAP_OK:0;RET:0;RETMSG:http://www.webxml.com.cn