zoukankan      html  css  js  c++  java
  • gsoap生成webservice调用客户端接口

    1.下载gsoap2.8

    2.运行

    wsdl2h.exe -o XXX.h XXX.wsdl

    wsdl文件可以是本地文件,也可以是服务器的wsdl,比如http://192.168.0.122:3333/ws/uss?wsdl

    3.生成客户端代码

    soapcpp2.exe  -L -x -C XXX.h -I .gsoap-2.8gsoapimport

    4.如果有多个服务,那么就将要使用命名空间

    wsdl2h -nns别名 -N服务命名空间 -o XXX.h XXX.wsdl
    wsdl2h -nuseraddns -NUserAddWS -oUserAddWebService.h userAddWebService.wsdl
    wsdl2h -nuserloginns -NUserLoginWS -oUserLoginWebService.h userLoginWebService.wsdl

    手动将生成的.h合并为一个新文件,比如上面两个User*.h,合并为新文件all.h,对照着很容易看出不同,把命名空间,类声明等合在一起就行了

    soap2cpp -LCx -pCMSWS All.h -I ../../import


    拷贝gsoap2.8目录下的stdsoap2.h,stdsoap2.cpp到程序目录,并修改stdsoap2.cpp文件,将其中一个.nsmap文件包含进去

    #include "xxx.nsmap"

    5.传输中文

    调用方法转为utf-8传输soap_set_mode( soap*, SOAP_C_UTFSTRING )

    如果用qt的QString将转换

    std::string CUIUtils::convertQStrToUTF8Str(const QString& value) {
        QByteArray arr =  value.toUtf8();
        return std::string(arr.data());
    }
     
    QString CUIUtils::convertUTF8StrToQStr(const std::string& value) {
        return QString::fromUtf8(value.c_str());
    }

    例子

    int CUserDetailInfoWebServiceImpl::getUserInfo(const char* address, CUserDetailInfo* userDetailInfo)
    {
        UserDetailInfoWebServiceCXFImplServiceSoapBinding webServiceBinding;
        if ((NULL != address) && strlen(address) > 0)
        {
            webServiceBinding.endpoint = address;
        }
        soap_set_mode(webServiceBinding.soap, SOAP_C_UTFSTRING);
        userDetailInfo->setRetCode(RET_CODE_ERROR_UNKNOWN);
        UserDetailInfoWS__getUserInfoByUserId request;
        std::string id = userDetailInfo->getUserId();
        request.userId = &id;
        UserDetailInfoWS__getUserInfoByUserIdResponse response;
        int ret = webServiceBinding.__UserDetailInfoWS__getUserInfoByUserId(&request, &response);
        if (SOAP_OK == ret)
        {
            if (NULL == response.result)
            {
                userDetailInfo->setRetCode(RET_CODE_NULL_OBJECT);
                userDetailInfo->setErrorDesc("no return value");
                return userDetailInfo->getRetCode();
            }
            userDetailInfo->setRetCode(response.result->retCode);
            if (RET_CODE_SUCCESS != userDetailInfo->getRetCode())
            {
                userDetailInfo->setErrorDesc(*response.result->desc);
            }
            else
            {
                if (NULL == response.result->userOperateInfo)
                {
                    userDetailInfo->setRetCode(RET_CODE_NULL_OBJECT);
                    userDetailInfo->setErrorDesc("no return info");
                    return userDetailInfo->getRetCode();
                }
                userDetailInfo->setDescript(*response.result->userOperateInfo->descript);
                userDetailInfo->setDepartmentId(*response.result->userOperateInfo->departmentId);
                userDetailInfo->setEnabled(response.result->userOperateInfo->enable);
                userDetailInfo->setLoginName(*response.result->userOperateInfo->loginName);
                userDetailInfo->setPassword(*response.result->userOperateInfo->password);
                userDetailInfo->setUserName(*response.result->userOperateInfo->name);
            }
     
        }
        else
        {
            userDetailInfo->setRetCode(RET_CODE_SOAP_ERROR);
            userDetailInfo->setDescript(*soap_faultstring(webServiceBinding.soap));
        }
        return userDetailInfo->getRetCode();
     
    }

    6.导入多个wsdl文件

    wsdl2h.exe -sck -t e:testtypemap.dat -o onvif.h analytics.wsdl analyticsdevice.wsdl deviceio.wsdl devicemgmt.wsdl display.wsdl event.wsdl imaging.wsdl media.wsdl ptz.wsdl Receiver.wsdl Recording.wsdl remotediscovery.wsdl Replay.wsdl Search.wsdl

    7.操作参数:

    以下就是wsdl2h的选项:

    -o 文件名,指定输出头文件
    -n 名空间前缀 代替默认的ns
    -c 产生纯C代码,否则是C++代码
    -s 不要使用STL代码
    -t 文件名,指定type map文件,默认为typemap.dat
    -e 禁止为enum成员加上名空间前缀
    type map文件用于指定SOAP/XML中的类型与C/C++之间的转换规则,比如在wsmap.dat里写
    xsd__string = | std::wstring | wchar_t*
    那么SOAP/XML中的string将转换成std::wstring或wchar_t*,这样能更好地支持中文。
    接着就是讲.h文件生成.cpp文件
    soapcpp2.exe接的选项如下
    -C 仅生成客户端代码
    -S 仅生成服务器端代码
    -L 不要产生soapClientLib.c和soapServerLib.c文件
    -c 产生纯C代码,否则是C++代码(与头文件有关)
    -I 指定imp<wbr>ort路径(见上文) </wbr>
    -x 不要产生XML示例文件
    -i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。
    这里一般需要带上-x,不然会生成一大堆的xml文件。
    -i选项也要带上,不然不会生成soapXXXXBindingService.cpp和soapXXXXBindingService.h文件
    -I 选项一般也要带上,-I 后接gsoap路径中import目录
    一般是-I E:workspaceonvifgsoap-2.8gsoapimport;E:workspaceonvifgsoap-2.8gsoap这样的

    转自:https://blog.csdn.net/johnnywww/article/details/8187398

  • 相关阅读:
    MongoDB+Lucence.net
    hubble+sqlserver
    C# 设计模式 1 接口模式 1.1 适配器模式 IT
    SQLServer2005 中 XML类型方法中 XQuery中变量的参数化匆忙整理 IT
    DoNET 类库设计准则01 名称规则 IT
    GMRES在matlab中的描述
    矩阵良态与病态
    调试vc++的一点感悟
    基于GramSchmidt正交法的广义极小残量法(GMRES)
    VC6 vs2003 vs2005 使用技巧(转)
  • 原文地址:https://www.cnblogs.com/liushui-sky/p/10831527.html
Copyright © 2011-2022 走看看