cmd 调用webservice接口_C++ 调用 Webservice gSoap 方法
大體思路是:使用gsoap生成文件,把生成的相關(guān)文件均加載到項目中調(diào)用
本文以調(diào)用天氣的webservice為例子,手把手教你。
步驟一
準備gSOAP工具:將gsoap_2.8.100.zip解壓,進入gsoap_2.8.100gsoap-2.8gsoapbinwin32目錄下,或win64目錄下。(工具可以在這個網(wǎng)址下載:https://sourceforge.net/projects/gsoap2/files/gsoap-2.8/)
圖1. 解壓后要進入的目錄步驟二
準備要調(diào)用的webservice接口的wsdl地址,比如網(wǎng)上的查詢天氣接口:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
打開鏈接是這個樣子:
圖2. xml文件右擊這網(wǎng)頁,選擇“另存為”到win32目錄下,直接把文件名后綴改成.wsdl,如下:
圖3. 另存為文件后的目錄步驟三
在win32目錄下新建一個文件”wsmp.dat“,文件內(nèi)容為xsd__string = | std::wstring | wchar_t*
圖4. wsmap.dat那么SOAP/XML中的string將轉(zhuǎn)換成std::wstrin或wchar_t*,這樣能更好地支持中文。
步驟四
在該目錄下打開cmd窗口,輸入以下命令。回車后WeatherWS.h頭文件
wsdl2h -s -t wsmap.dat WeatherWS.wsdl- -o 文件名,指定輸出頭文件
- -n 名空間前綴 代替默認的ns
- -c 產(chǎn)生純C代碼,否則是C++代碼
- -s 不要使用STL代碼
- -t 文件名,指定type map文件,默認為typemap.dat
- -e 禁止為enum成員加上名空間前綴
步驟五
解析WeatherWS.h,生成存根程序
在該目錄下打開cmd窗口,輸入以下命令。
soapcpp2 -i -C -x -L WeatherWS.h -I D:gsoap_2.8.108gsoap-2.8gsoapimport注意D:gsoap_2.8.108gsoap-2.8gsoapimport是我的路徑,根據(jù)你的路徑來變。
此時會生成6個文件,如下圖:
- -C 僅生成客戶端代碼
- -S 僅生成服務(wù)器端代碼
- -L 不要產(chǎn)生soapClientLib.c和soapServerLib.c文件
- -c 產(chǎn)生純C代碼,否則是C++代碼(與頭文件有關(guān))
- -I 指定import路徑(此項是必要的,因前面為指定-s)
- -x 不要產(chǎn)生XML示例文件
- -i生成C++包裝,客戶端為xxxxProxy.h(.cpp),服務(wù)器端為xxxxService.h(.cpp)。
步驟六
打開Visual Studio 2017,在里面新建一個空項目。我在這里命名為“TestWebservice”。
將文件復(fù)制到vs的項目目錄下,選擇“添加現(xiàn)有項”,添加好的樣子如下:
其中的stdsoap2.h和stdsoap2.cpp在gsoap_2.8.100gsoap-2.8gsoap目錄下
在“源文件”目錄下,右擊新建一個.cpp文件添加main函數(shù)。空項目默認啥都沒有。
#include <stdio.h> #include <stdlib.h> #include <fstream> #include < string > #include <iostream>#include <iostream> //包含soap頭文件 #include "soapH.h" #include "soapStub.h" #include "WeatherWSSoap.nsmap" #include "soapWeatherWSSoapProxy.h"using namespace std;void main(int argc, char **argv) {//WebService的請求地址const char* web_url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";//soap接口WeatherWSSoapProxy soap(SOAP_C_UTFSTRING);//構(gòu)造輸入?yún)?shù)_ns1__getWeather city_name;city_name.theCityCode = L"";city_name.theUserID = L"";//輸出參數(shù)_ns1__getWeatherResponse weather_res;//調(diào)用接口方法getWeather int xlt = soap.getWeather(web_url, NULL, &city_name, weather_res);//判斷接口返回值, SOAPOK表示成功if (xlt == SOAP_OK){// 獲取返回結(jié)果ns1__ArrayOfString* aos = weather_res.getWeatherResult;// 打印返回結(jié)果int count = aos->__sizestring;for (int i = 0; i < count; i++){std::wstring a = (aos->string)[i];wcout.imbue(locale("chs"));wcout << a.c_str() << endl;}}getchar(); }錯誤修改
會有紅色下劃線錯誤提示
錯誤提示右擊項目“屬性”->c/c++->語言->符合模式,改成否。如下圖:
最后的結(jié)果
最后輸出原創(chuàng)不易,歡迎評論,并點贊!!
Enjoy!!!
總結(jié)
以上是生活随笔為你收集整理的cmd 调用webservice接口_C++ 调用 Webservice gSoap 方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 扫掠两条引导线_如何巧用引导线,把摄影水
- 下一篇: idea启动tomcat很慢_idea使