您的位置:首页 > 编程语言 > C语言/C++

c++ 通过 gsoap实现 webservice(附带天气实现的例子)

2015-03-05 15:14 351 查看
前些日子,客户那边需要做2个接口,都是通过 webservice访问,因为是第一次接触,所以只能度娘呗,网上找了一大堆各种调用 webservice的例子,开始试了几个都没有成功,后来同事测试成功了一个简单的例子,才发现比较简单。下面就 写下 我开发的步骤,并实现天气的获取。

1、首先在网上下载一个gsoap, 我临时下了个gsoap-2.8版点击打开链接的,找到soapcpp2.exe和wsdl2h.exe ,在dos中进入其目录

2、wsdl2h.exe -o weather.h http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL 会生成weather.h
,不知道为什么这么多警告,我用gsoap-2.7版本的时候,貌似没有。不过,不要在意这些细节,我们继续哈,soapcpp2.exe -C weather.h -ID:\ggsoap-2.8\gsoap\import(因为,这里需要import中的一个文件,才能编译过,也可以将那个文件放在当前目录下再编译,也ok)。这样就生成了可用的c++文件了soapC.cpp、soapClient.cpp、soapClientLib.cpp、soapH.h、soapStub.h以及WeatherWSSoap.nsmap。前些日子,还生产**SoapProxy.h文件,这些日子每次都没有,不知道啥原因,不过,不要在意这些细节。这些文件(主要是在在soapClient.cpp)里面封装了我们需要的接口,可以在http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx中查看相关接口。

3、新建控制台程序,并将soapC.cpp、soapClient.cpp、soapH.h、soapStub.h以及WeatherWSSoap.nsmap加入项目中去,soapClientLib.cpp不需要加,貌似会报错。

4、代码实现,

#include <iostream>

#include <string>

#include "gsoap\WeatherWSSoap.nsmap"

#include <tchar.h>

#include <Windows.h>

using namespace std;

//UTF-8到GB2312的转换

char* U2G(const char* utf8)

{

int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);

wchar_t* wstr = new wchar_t[len+1];

memset(wstr, 0, len+1);

MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);

len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);

char* str = new char[len+1];

memset(str, 0, len+1);

WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);

if(wstr) delete[] wstr;

return str;

}

//GB2312到UTF-8的转换

char* G2U(const char* gb2312)

{

int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);

wchar_t* wstr = new wchar_t[len+1];

memset(wstr, 0, len+1);

MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);

len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);

char* str = new char[len+1];

memset(str, 0, len+1);

WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);

if(wstr) delete[] wstr;

return str;

}

int main(int argc, char* argv[])

{

struct soap soap;

soap_init(&soap);

soap_set_mode(&soap,SOAP_C_UTFSTRING); //设置soap编码为UTF-8,防止中文乱码

/*******

*通过网页输入http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionCountry验证

**无参数******************/

_ns1__getRegionCountry ns1__getRegionCountry;

_ns1__getRegionCountryResponse ns1__getRegionCountryResponse;

soap_call___ns1__getRegionCountry(&soap, "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL","",&ns1__getRegionCountry, ns1__getRegionCountryResponse);

char www[1024]="";

if(soap.error)

{

soap_print_fault(&soap, stderr);

}

else

{

ns1__ArrayOfString *aos = ns1__getRegionCountryResponse.getRegionCountryResult;

for(vector<string>::iterator

itr=aos->string.begin(), itr_end = aos->string.end();

itr!=itr_end; ++itr)

{

string str = *itr;

const char* str2 =str.c_str();

std::cout << U2G(str2) << std::endl;

}

soap_set_test_logfile(&soap,www);

}

getchar();

/*********有参数*****

*可以通过网页输入http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theRegionCode=31113对比

*************/

_ns1__getSupportCityString ns1__getSupportCityString ; //参数类

_ns1__getSupportCityStringResponse ns1__getSupportCityStringResponse;//返回的结果的类

std::string city = "31113";//陕西的id,通过http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince可以获得

ns1__getSupportCityString.theRegionCode=&city;//因为_ns1__getSupportCityString内的指针成员貌似都未分配空间,所以我们给其赋值时,不要直接赋值

soap_call___ns1__getSupportCityString(&soap, "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL","", &ns1__getSupportCityString, ns1__getSupportCityStringResponse);

if(soap.error)

{

soap_print_fault(&soap, stderr);

}

else

{

ns1__ArrayOfString *aos = ns1__getSupportCityStringResponse.getSupportCityStringResult;

for(vector<string>::iterator

itr=aos->string.begin(), itr_end = aos->string.end();

itr!=itr_end; ++itr)

{

string str = *itr;

const char* str2 =str.c_str();

std::cout << U2G(str2) << std::endl;

}

soap_set_test_logfile(&soap,www);

}

getchar();

return 0;

}

效果图:





总结:所有的接口不管基本上都是上面几步,就可以搞定了,我个人主要注意这几点,

1、了解方法及相关参数,在soapClient.cpp中找到相关方法的接口,一般都是在原方法名前面多了某些前缀而已,并查看其需要的参数。一般来讲基本是5个,第一是 soap结构,第二个是WSDL,第三个给空就ok,第四个是参数(一般是类,跟进入可以看看里面的结构,这样可以更好的理解),第5个参数是返回值,也是一个类(这里要注意,比较复杂的返回值,里面会有多个指针类型,需要我们提前初始化);

2、如果调用出现错误,可以通过我代码中那里打印的错误信息查看,但是一般不太容易,更好的就是可以进入你调用的方法中去,调试,里面的方法层次基本相同,可以看返回的东东。最好的话可以装个抓包工具。

最后最后再吐槽一下,csdn的编辑器真的不好用啊。希望能改进改进
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: