qt wsdl 接口调用(gsoap)

这篇博客介绍了如何在Qt环境中利用gSOAP库调用WSDL接口的详细步骤,包括下载gSOAP、生成接口文件、配置Qt工程以及处理调用接口时的编码问题。作者指出该过程相对C#来说较为繁琐,当接口变更时需要重新生成接口文件,操作不便。
摘要由CSDN通过智能技术生成

qt  wsdl的接口调用真没c# 好用。c#一分钟就能搞定,qt 呵呵。。。

算了,开始正题:

第一步:下载 gsoap

1 、 下载链接:https://www.genivia.com/products.html,下载开源版本

 2、 解压文件 ,将解压后的文件随便放个地方

第二步、 用gsoap生成接口文件

1 、用电脑的 “命令处理器” (cmd.exe)

2.、将cmd路径指定到   gsoap_2.8.122\gsoap-2.8\gsoap\bin\win64 下

 3、生成接口包含文件 “myESealSoap.h”,

输入 : wsdl2h -o myESealSoap.h  http://xxxxxxxxxxx/ImpExpService?wsdl

会在 gsoap_2.8.122\gsoap-2.8\gsoap\bin\win64 下面生成 myESealSoap.h 文件。

wsdl2h : gsoap的程序

myESealSoap.h: 包含接口的文件,自己随便起个

http://xxxxx/ImpExpService?wsdl : 接口url

 4、 生成可调用接口文件

继续输入: soapcpp2 myESealSoap.h -I C:\gsoap_2.8.122\gsoap-2.8\gsoap\import

C:\gsoap_2.8.122\gsoap-2.8\gsoap\import :  这个根据自己放的位置修改,一定要指定到 import 文件

 

 

 5、 QT程序目录下面建个文件夹, 将上面这些文件拷贝过去,  并将 gsoap_2.8.122\gsoap-2.8\gsoap 下的 stdsoap2.cpp  stdsoap2.h 拷贝进去

 

 最终需要的文件就是这些

第三步: qt工程调用接口

1. 在 .pro 文件中 添加

SOURCES += \
    myGsoap/soapC.cpp \
    myGsoap/soapClient.cpp \
    myGsoap/stdsoap2.cpp\

HEADERS += \
    myGsoap/soapH.h \
    myGsoap/soapStub.h \
    myGsoap/stdsoap2.h

INCLUDEPATH += ./myGsoap

LIBS += -lws2_32      # 这个特别重要,一定要加

 2. 在主程序中,添加 头文件

#include "myGsoap/soapH.h"
#include "myGsoap/ImpExpServiceSoap11Binding.nsmap"
#include "myGsoap/soapStub.h"
#include "myGsoap/stdsoap2.h"

3. 调用接口 

void MainWindow::on_pushButton_clicked()
{
    struct soap m_soap;
    soap_init(&m_soap);
    soap_set_mode(&m_soap,SOAP_C_UTFSTRING);  //解决gsoap中文乱码问题
    _ns1__eUserLogin req;          // 传递数据, 和接口名对应  _ns1__xxxxx
    _ns1__eUserLoginResponse res;  // 接收数据, 和接口名对应  _ns1__xxxxxResponse

    // 生成json 数据
    QJsonObject jsonObj;
    jsonObj.insert ("userAccount",ui->userName->text().trimmed());
    jsonObj.insert ("password",ui->passWord->text().trimmed());
    QJsonDocument document(jsonObj);
    std::string jsonString = document.toJson(QJsonDocument::Compact).toStdString();  // json 转为std::string
    req.jsonPara = &jsonString;  // 添加要传递的参数 (根据接口参数格式传递)

    qDebug() << document.toJson(QJsonDocument::Compact);

    // 调用接口 soap_call__ns1__接口名称();   soap_call__ns1  这个是固定的
    if (soap_call___ns1__eUserLogin(&m_soap,NULL,NULL, &req, res) == SOAP_OK)
    {
        qDebug()<<"接口调用成功";
    }

    if(m_soap.error != 0){
        //获取错误提示
        QString errStr = QString::fromLocal8Bit(*soap_faultstring(&m_soap));
        qDebug()<< "错误提示: "  << errStr;
    }
    else {
        //获取结果字符串
        QByteArray strRes = res.return_->c_str();

        // 拆解json
        QJsonDocument doc =QJsonDocument::fromJson(strRes);
        if(doc.isObject())
        {
            QJsonObject obj =doc .object();
            qDebug()<< obj.value("retMsg").toString();
        }

        // 打印整个返回结果
        qDebug()<<QTextCodec::codecForName("utf8")->toUnicode(strRes);
    }
    soap_end(&m_soap);
    soap_done(&m_soap);
}

最后:

 这个东西真心难用, 如果接口发生改变, 就得重新生成接口文件, 麻烦的要死

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C#调用wsdl接口的三种方法示例: 1. 使用Visual Studio自动生成代理类: ```csharp // 引用WebService接口 using WebServiceNamespace; // 创建代理类实例 WebServiceClient client = new WebServiceClient(); // 调用接口方法 string result = client.MethodName(parameter); ``` 2. 使用命令行工具wsdl.exe生成代理类: ```shell // 打开命令提示符 // 进入wsdl.exe所在目录 // 执行以下命令生成代理类 wsdl /language:c# /out:生成类的物理路径 /url:WebService接口URL或wsdl文件路径 // 在代码中引用生成的代理类 using WebServiceNamespace; // 创建代理类实例 WebServiceClient client = new WebServiceClient(); // 调用接口方法 string result = client.MethodName(parameter); ``` 3. 手动解析wsdl文件: ```csharp // 引用System.Web.Services和System.Web.Services.Description命名空间 using System.Web.Services; using System.Web.Services.Description; using System.Xml; // 创建WebService描述文件的URL string wsdlUrl = "WebService接口URL或wsdl文件路径"; // 创建ServiceDescription对象 ServiceDescription serviceDescription = ServiceDescription.Read(wsdlUrl); // 创建ServiceDescriptionImporter对象 ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); importer.ProtocolName = "Soap"; // 指定协议为Soap // 添加ServiceDescription对象 importer.AddServiceDescription(serviceDescription, null, null); // 创建CodeNamespace对象 CodeNamespace codeNamespace = new CodeNamespace("WebServiceNamespace"); // 创建CodeCompileUnit对象 CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); codeCompileUnit.Namespaces.Add(codeNamespace); // 生成代理类代码 ServiceDescriptionImportWarnings warnings = importer.Import(codeNamespace, codeCompileUnit); // 使用CodeDomProvider编译代理类 CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp"); CompilerParameters compilerParameters = new CompilerParameters(); compilerParameters.GenerateExecutable = false; compilerParameters.GenerateInMemory = true; CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, codeCompileUnit); // 获取生成的代理类类型 Type proxyType = compilerResults.CompiledAssembly.GetTypes().FirstOrDefault(t => t.Name == "WebServiceClient"); // 创建代理类实例 object proxyInstance = Activator.CreateInstance(proxyType); // 调用接口方法 MethodInfo method = proxyType.GetMethod("MethodName"); string result = (string)method.Invoke(proxyInstance, new object[] { parameter }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值