Qt 使用 gsoap 访问 webservice

客户端代码生成

  1. 下载 gsoap
    gsoap 下载地址

  2. 生成头文件
    执行以下命令, 根据 wsdl 路径生成 头文件:
    wsdl2h -s -o xxxx.h http://xxxxxxx.asmx?wsdl

  3. 生成代理类
    ①.从 gsoap 目录拷贝 typemap.dat 到 bin 文件夹
    ②.在 typemap.dat 末尾增加以下内容,以更好支持中文
    xsd__string = | std::wstring | wchar_t*
    ③.执行以下命令,生成代理类:
    soapcpp2 -d gsoap -i -C -x -IF:\gsoap_2.8.106\gsoap-2.8\gsoap\import xxxx.h
    ④.从 gsoap 目录拷贝 stdsoap2.cpp、stdsoap2.h 到生成的文件夹
    在这里插入图片描述

Qt 项目使用

  1. 添加以上生成的文件到当前项目
  2. pro 中增加一下内容:
    LIBS+= D:\Qt\Qt5.14.2\Tools\mingw730_32\i686-w64-mingw32\lib\libws2_32.a
  3. 主程序调用即可
#include <QCoreApplication>
#include "updataServiceSoap.nsmap"
#include "soapupdataServiceSoapProxy.h"
#include <QDebug>
using namespace std;

string wstring2string(wstring wstr)
{
    string result;
    //获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
    int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
    char* buffer = new char[len + 1];
    //宽字节编码转换成多字节编码
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
    buffer[len] = '\0';
    //删除缓冲区并返回值
    result.append(buffer);
    delete[] buffer;
    return result;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    updataServiceSoapProxy * updateservice = new updataServiceSoapProxy("http://192.168.1.107:190/updataService.asmx");


    _ns1__HelloWorld helloWorld;
    _ns1__HelloWorldResponse helloWorldResponse;
    wchar_t wt[] = L"你好,世界abchd1234";
    helloWorld.helloworld = wt;
    updateservice->HelloWorld(&helloWorld, helloWorldResponse);

    wstring wstr = helloWorldResponse.HelloWorldResult;
    string res = wstring2string(wstr);
    cout << res;

    return a.exec();
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值