C++webservice接口调用

一、WebService例子

1.准备要调用的webservice接口的wsdl地址,比如网上的查询天气接口://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目录下(工具可以在这个网址下载://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 = "//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接口地址://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