windows qt 使用gsoap访问 WSDL 网络接口,以天气预报网站为示例

gsoap官方文档: https://www.genivia.com/docs.html
Web服务URL:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx
web服务WSDL:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL
1. gsoap 下载:
2.用gsoap生成接口文件
  • 打开cmd.exe
  • 进入 gsoap的解压目录 如:C:\Users\admin\Documents\projects\Beier-Pro\gsoap-2.8\gsoap\bin\win32(gsoap 有win64、win32等版本,建议使用win32)
  • 输入命令生成 接口文件 : myESSoap.h(可自定义文件名称)
    wsdl2h -o myESSoap.h http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL
    在这里插入图片描述
  • 生成可调用的接口文件:
    命令:soapcpp2.exe myESSoap.h -I ..\..\import
    在这里插入图片描述
    或者使用 命令:soapcpp2.exe -i -x -C myESSoap.h -I ..\..\import 直接生成 xxxproxy.cpp xxxproxy.h
    在这里插入图片描述

在这里插入图片描述

脚本一键生成

脚本目录放在 bin\win32 或 bin\win64 目录中,文件名为 xxx.bat,双击运行

echo 生成头文件Service.h
wsdl2h.exe -o Service.h http://xxx/Services/InterFace/InterFaceService.svc?WSDL
 
echo 生成其它soap文件支持
soapcpp2.exe -I../../import -x -L -i -C Service.h -1
 
del OutFile /s /Q
MD OutFile
echo 拷贝文件
copy ..\..\stdsoap2.h OutFile\ 
copy ..\..\stdsoap2.cpp OutFile\ 
copy *.h OutFile\ 
copy *.cpp OutFile\ 
copy *.nsmap OutFile\
 
echo 删除临时文件
del /f/q .\*.h
del /f/q .\*.cpp
del /f/q .\*.nsmap
 
pause
3.将生成的文件添加至项目中
  • 在qt 工程中创建文件夹,将上一步生成接口文件目录中,除wsdl2h.exe、soapcpp2.exe、myESSoap.h之外的其它文件都拷贝至此目录。
  • 拷贝 gsoap_2.8.122\gsoap-2.8\gsoap 下的 stdsoap2.cpp stdsoap2.h 至上一步所建目录中。
  • 拷贝之后项目目录结构如下:
    在这里插入图片描述
  • 修改打开qt creator,修改.pro文件,将以下几个文件引入项目中:
SOURCES += \
    gSoapWheather/soapC.cpp \
    gSoapWheather/soapClient.cpp \
    gSoapWheather/stdsoap2.cpp \
    main.cpp \
    mysoap.cppS


HEADERS += \
    mysoap.h \
    gSoapWheather/myESSoap.h \
    gSoapWheather/soapH.h \
    gSoapWheather/soapStub.h \
    gSoapWheather/stdsoap2.h

INCLUDEPATH += ./gSoapWheather
  • 加载ws2_32.lib库,在.pro文件中最后面添加如下代码:
LIBS += -lws2_32    

*main.cpp

#include <QCoreApplication>
#include "mysoap.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MySoap w;

    return a.exec();
    
}
  • mysoap.cpp
#include "mysoap.h"
#include "gSoapWheather/soapH.h"
#include "gSoapWheather/soapStub.h"
#include "gSoapWheather/stdsoap2.h"
#include "gSoapWheather/WeatherWSSoap.nsmap"
#include <QtDebug>

MySoap::MySoap(QObject *parent)
    : QObject(parent)
{
    struct soap m_soap;
    soap_init(&m_soap);
    soap_set_mode(&m_soap, SOAP_C_UTFSTRING); //解决gsoap中文乱码问题
    _ns1__getSupportCityString req;           // 传递数据, 和接口名对应  _ns1__xxxxx
    _ns1__getSupportCityStringResponse res;   // 接收数据, 和接口名对应  _ns1__xxxxxResponse

    QString url("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
    QString acton("http://www.w3.org/2003/05/soap-envelope");
    std::string str = "shanghai";
    req.theRegionCode = &str; //接口参数
    int ErrNo = soap_call___ns1__getSupportCityString(&m_soap, url.toUtf8(), acton.toUtf8(), &req, res);

    if (ErrNo == SOAP_OK) {
        foreach (auto val, res.getSupportCityStringResult->string) {
            qDebug() << "res:" << val.data();
        }
    }
    if (m_soap.error != 0) {
        //获取错误提示
        QString errStr = QString::fromLocal8Bit(*soap_faultstring(&m_soap));
        qDebug() << "错误提示: " << errStr << "error code:" << m_soap.error;
    }
    soap_end(&m_soap);
    soap_done(&m_soap);
}

参考:https://blog.csdn.net/chen1231985111/article/details/126125568,感谢!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值