1. 程式人生 > >C++webservice介面呼叫

C++webservice介面呼叫

一、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,除非編譯器找不到標頭檔案

第三步開始程式設計,我只寫了個測試的主函式:

 

  • #include "soapMobileCodeWSSoapProxy.h"

  •   #include "MobileCodeWSSoap.nsmap"
  •   #include <iostream>
  •   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