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

常用的webservice接口点击打开链接

前些日子,客户那边需要做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的编辑器真的不好用啊。希望能改进改进

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于gsoap2.8实现IPv6设备搜索的C++代码示例: 服务端代码: ```c++ #include "soapH.h" #include "DeviceService.nsmap" #include <stdio.h> int main(int argc, char **argv) { struct soap soap; soap_init(&soap); if (soap_bind(&soap, "::1", 8080, 100) < 0) // 在IPv6下绑定地址 { soap_print_fault(&soap, stderr); exit(-1); } printf("DeviceService ready at %s\n", soap.wsdd); while (true) { if (soap_serve(&soap) != SOAP_OK) { soap_print_fault(&soap, stderr); exit(-1); } soap_destroy(&soap); soap_end(&soap); } soap_done(&soap); return 0; } int DeviceService__GetDeviceInfo(struct soap *soap, char *deviceID, char *deviceInfo) { sprintf(deviceInfo, "Device ID: %s", deviceID); return SOAP_OK; } ``` 客户端代码: ```c++ #include "soapH.h" #include "DeviceService.nsmap" #include <stdio.h> int main(int argc, char **argv) { struct soap soap; soap_init(&soap); soap_set_namespaces(&soap, namespaces); // 初始化命名空间 soap_set_mode(&soap, SOAP_C_UTFSTRING); // 设置通信方式为UTF-8字符串 DeviceServiceBindingProxy proxy; proxy.soap_endpoint = "http://[::1]:8080"; // 创建设备搜索请求消息 _tds__GetDeviceInformation request; _tds__GetDeviceInformationResponse response; if (proxy.GetDeviceInformation(&request, &response) == SOAP_OK) // 发送设备搜索请求 { printf("Device information: %s\n", response.DeviceInformation); } else { soap_print_fault(&soap, stderr); } soap_destroy(&soap); soap_end(&soap); soap_done(&soap); return 0; } ``` 需要注意的是,服务端代码需要实现`DeviceService__GetDeviceInfo`函数,该函数用于返回设备信息。客户端代码中的请求消息和响应消息需要根据实际情况进行修改。此外,客户端代码中的SOAP_ENDPOINT需要修改为服务端的IPv6地址和端口号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值