C++ (使用gsoap)调用 WCF服务

本文介绍如何使用gsoap工具让C++程序调用WCF发布的basicHttpBinding服务,包括生成代理类和服务调用代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:c++通过使用gsoap调用wcf发布的basicHttpBinding服务。

 (转载请注明来源:cnblogs coder-fang)

 

 

  1. 创建WCF服务并启动,这里不多说,请参考 https://www.cnblogs.com/coder-fang/p/6594964.html  ,确定服务接口已发布,浏览器可访问wsdl,如下(我创建的示例服务提供basicHttpBinding 和 netTcpBinding两种方式):

     

  2. 下载gSoap工具,下载地址:http://sourceforge.net/projects/gsoap2   ,解压至指定目录,我这里放在 D:\Program Files (x86)\gsoap-2.8
  3. 进入 D:\Program Files (x86)\gsoap-2.8\gsoap\bin\win32  ,创建 wsmap.dat,文件里输入: xsd__string = | std::wstring | wchar_t*
  4. 在此目录下运行两个cmd命令:
    1.   运行如下命令生成 service.h
      wsdl2h.exe -s -t wsmap.dat -o service.h http://localhost:9999/?wsdl

    2. 运行如下命令,生成相关代理类:
      soapcpp2.exe -C -I"D:\Program Files (x86)\gsoap-2.8\gsoap\import" -L -i -x -1 service.h

    3. 此时目录会多出几个c++文件:

       

  5.   创建C++控制台程序,将以上文件复制到工程中,并同时复制 stdsoap2.h 与 stdsoap2.cpp (在gsoap\import目录下),使工程包含各文件,同时将工程设置为 不使用预编译:

     

  6. 主函数文件代码如下:
    // C_Client.cpp: 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    
    #include <stdlib.h>
    #include <iostream>
    
    #include "soapBasicHttpBinding_USCOREIServiceProxy.h"
    #include "BasicHttpBinding_USCOREIService.nsmap"
    #pragma warning(disable : 4996)
    
    std::string WString2String(const std::wstring& ws)
    {
        std::string strLocale = setlocale(LC_ALL, "");
        const wchar_t* wchSrc = ws.c_str();
        size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
        char *chDest = new char[nDestSize];
        memset(chDest, 0, nDestSize);
        wcstombs(chDest, wchSrc, nDestSize);
        std::string strResult = chDest;
        delete[]chDest;
        setlocale(LC_ALL, strLocale.c_str());
        return strResult;
    }
    // string => wstring
    std::wstring String2WString(const std::string& s)
    {
        std::string strLocale = setlocale(LC_ALL, "");
        const char* chSrc = s.c_str();
        size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1;
        wchar_t* wchDest = new wchar_t[nDestSize];
        wmemset(wchDest, 0, nDestSize);
        mbstowcs(wchDest, chSrc, nDestSize);
        std::wstring wstrResult = wchDest;
        delete[]wchDest;
        setlocale(LC_ALL, strLocale.c_str());
        return wstrResult;
    }
    int main()
    {
        
        const char* addr = "http://localhost:9999/Service";
        BasicHttpBinding_USCOREIServiceProxy proxy(addr, SOAP_C_UTFSTRING);
        _ns1__GetUsersResponse rsp;
        _ns1__GetUsers getData;
        
        wchar_t start[] = L"z";//返回以 z 开头的名称
        getData.startWith = start;
        
        
        
        if (proxy.GetUsers(&getData, rsp) == SOAP_OK)
        {
            for (int i = 0; i < rsp.GetUsersResult->__sizestring; i++)
            {
                printf("%s \n", WString2String(*rsp.GetUsersResult->string++).c_str());
            }    
        }
        else
        {
            proxy.soap_stream_fault(std::cerr);
        }
        proxy.destroy();
        
        return 0;
    }
    View Code

     

     

  7. 运行效果:

     

     

  8. 目前只试过使用http绑定模式,tcp绑定暂时未通过,正在尝试中。

转载于:https://www.cnblogs.com/coder-fang/p/9634551.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值