使用gSOAP与WebService - 第二部分 开发第一个WebService客户端(C++)

2023-11-11

CurrencyConvertor - How use gSOAP and WebServices - Part 2 Doing the first WS client.

Introduction

This is the second of two articles where I explain building a web service client from wsdl file. With Part One, I explained how to get the class from the wsdl file to be used in VC++ 6. In Part Two, I will show you how to use the classes generated with gSOAP in Part One.

Using the code

Usually in the file soap*****************SoapProxy.h we can find the class definition of generic WS. In this case, soapCurrencyConvertorSoapProxy.h, I have already included it in Part One.

Collapse
//soapCurrencyConvertorSoapProxy.h

#ifndef soapCurrencyConvertorSoap_H
#define soapCurrencyConvertorSoap_H
#include "soapH.h"
class CurrencyConvertorSoap
{   public:
    struct soap *soap;
    const char *endpoint;
    CurrencyConvertorSoap()
    { soap = soap_new(); endpoint = 
        "http://www.webservicex.net/CurrencyConvertor.asmx";
        if (soap && !soap->namespaces) 
        { static const struct Namespace namespaces[] = 
{
    {"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", 
        "http://www.w3.org/*/soap-envelope", NULL},
    {"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", 
        "http://www.w3.org/*/soap-encoding", NULL},
    {"xsi", "http://www.w3.org/2001/XMLSchema-instance", 
        "http://www.w3.org/*/XMLSchema-instance", NULL},
    {"xsd", "http://www.w3.org/2001/XMLSchema", 
         "http://www.w3.org/*/XMLSchema", NULL},
    {"ns1", "http://www.webserviceX.NET/", NULL, NULL},
    {NULL, NULL, NULL, NULL}
};
    soap->namespaces = namespaces; } };
    virtual ~CurrencyConvertorSoap() { if (soap) { soap_destroy(soap); 
        soap_end(soap); soap_done(soap); soap_del(soap); } };
    virtual int __ns1__ConversionRate(_ns1__ConversionRate 
        *ns1__ConversionRate, _ns1__ConversionRateResponse     
        *ns1__ConversionRateResponse) 
        { return soap ? soap_call___ns1__ConversionRate(soap, 
            endpoint, NULL, ns1__ConversionRate, 
            ns1__ConversionRateResponse) : SOAP_EOM; };
};
#endif

In this case, the method is only one and it is __ns1__ConversionRate .

The class takes two params. The first is the input (the two currencies) and the second is the output (the result).

Before you use the class method of WS you must instantiate the param with soap_instantiate(), and with soap, from your CurrencyConvertor example.

pConversionRate = (_ns1__ConversionRate*) 
    soap_instantiate(m_pCurrencyConvertor->soap,
    SOAP_TYPE__ns1__ConversionRate,"",
    "",pConversionRateSize);
    
    and 

pConversionRateResponse = (_ns1__ConversionRateResponse*) 
    soap_instantiate(m_pCurrencyConvertor->soap, 
    SOAP_TYPE__ns1__ConversionRateResponse,/
        "","",pConversionRateResponseSize);

This returns an int that is SOAP_OK, if all is well. (Note: You can find all other definitions for errors in stdsopa2.h)

If this did not return SOAP_OK you can use soap_print_fault to get the error string.

In the file Soaph.h you can find methods you can use, like soap_s2ns1__Currency or soap_ns1__Currency2s. Just as the name explains, they are used to convert the currency to string and vice versa.

Using the code CMyCurrencyConvertor

For the test I have develop a new class CMyCurrencyConvertor that replaces the methods of CurrencyConvertor.

The class CMyCurrencyConvertor instantiates all the created pointers itself.

I have made only a few public methods and I use them in the dialog.

Constructor

In the constructor I dynamically allocate at run time the object CurrencyConvertorSoap.

CMyCurrencyConvertor::CMyCurrencyConvertor()
{
    m_pCurrencyConvertor= new CurrencyConvertorSoap;    
}

Destructor

In the destructor I delete the memory allocated for CurrencyConvertorSoap.

CMyCurrencyConvertor::~CMyCurrencyConvertor()
{    
    delete m_pCurrencyConvertor;    
}

Instantiate

I have two instantiates for the params, InstantiateRate and InstantiateRateResponse.

Collapse
_ns1__ConversionRate * CMyCurrencyConvertor::InstantiateRate()
{    
    _ns1__ConversionRate *pConversionRate;
    size_t *pConversionRateSize = new size_t;
    *pConversionRateSize = sizeof(_ns1__ConversionRate);

    pConversionRate = (_ns1__ConversionRate*)
        soap_instantiate(m_pCurrencyConvertor->soap,
        SOAP_TYPE__ns1__ConversionRate,"","",
        pConversionRateSize);
    
    delete pConversionRateSize ;

    return pConversionRate;
}

_ns1__ConversionRateResponse * CMyCurrencyConvertor::InstantiateRateResponse()
{    
    _ns1__ConversionRateResponse *pConversionRateResponse;
    size_t *pConversionRateResponseSize = new size_t;
    *pConversionRateResponseSize = sizeof(_ns1__ConversionRateResponse);

    pConversionRateResponse = (_ns1__ConversionRateResponse*) 
        soap_instantiate(m_pCurrencyConvertor->soap , /
        SOAP_TYPE__ns1__ConversionRateResponse,"",
        "",pConversionRateResponseSize);
    
    delete pConversionRateResponseSize ;

    return pConversionRateResponse;
}

GetConversionRate

I take two methods, one takes the input of string Code for currency and the other takes the type of code for currency.

Collapse
bool CMyCurrencyConvertor::GetConversionRate(ns1__Currency oFromCurrencyCode,
    ns1__Currency oToCurrencyCode, double &dRate)
{
    int iRet = SOAP_ERR;
    
    _ns1__ConversionRate            *pConversionRate;
    _ns1__ConversionRateResponse    *pConversionRateResponse;
    
    pConversionRate = InstantiateRate();
    pConversionRateResponse = InstantiateRateResponse();

    pConversionRate->FromCurrency = oFromCurrencyCode;
    pConversionRate->ToCurrency = oToCurrencyCode;

    iRet = m_pCurrencyConvertor->__ns1__ConversionRate(pConversionRate,
         pConversionRateResponse);
    
    switch(iRet)
    {
    case SOAP_OK :
        {
            dRate = pConversionRateResponse->ConversionRateResult;
            return true;
        }
            /*
        case SOAP_CLI_FAULT            
        case SOAP_SVR_FAULT            
        case SOAP_TAG_MISMATCH        
        case SOAP_TYPE            
        case SOAP_SYNTAX_ERROR        
        case SOAP_NO_TAG            
        case SOAP_IOB            
        case SOAP_MUSTUNDERSTAND        
        ...
        */
    default:
        {
            Log(GetSoapError());              
        }
    }    
    return false;
}

I create the two param pointers and instantiate it. I set the two currency code and call the ConversionRate.

If all works fine, I return the conversion, otherwise I return an error.

// Conversion from String code
bool CMyCurrencyConvertor::GetConversionRate(CString sFromCurrencyCode,
    CString sToCurrencyCode, double &dRate)
{
    ns1__Currency oFromCurrencyCode; 
    ns1__Currency oToCurrencyCode;

    int iRetFromCurrencyCode = soap_s2ns1__Currency
      (m_pCurrencyConvertor->soap,sFromCurrencyCode, 
      &oFromCurrencyCode);

    int iRetToCurrencyCode = soap_s2ns1__Currency
      (m_pCurrencyConvertor->soap,sToCurrencyCode, 
      &oToCurrencyCode);

    return GetConversionRate(oFromCurrencyCode,oToCurrencyCode,dRate);    
}

With this method you can call the function without knowing the internal type. For the conversion I use the function soap_s2ns1__Currency.

GetSoapError

I add another method for retrieving the error from soap to string.

Collapse
CString CMyCurrencyConvertor::GetSoapError()
{ 
    struct soap *pSoap = m_pCurrencyConvertor->soap;
    CString sError;
    if (soap_check_state(pSoap ))
        sError.Format("Error: soap struct not initialized/n");
    else
    {
        if (pSoap->error)
        { 
            const char *pFaultCode, *pFaultSubCode = NULL, *pFalutString, 
                **iFaultCode;
            iFaultCode = soap_faultcode(pSoap );
            if (!*iFaultCode)
                soap_set_fault(pSoap );
            pFaultCode = *iFaultCode;
            if (pSoap ->version == 2)
                pFaultSubCode = *soap_faultsubcode(pSoap );
            pFalutString = *soap_faultstring(pSoap );
            iFaultCode = soap_faultdetail(pSoap );
            sError.Format("%s%d fault: %s [%s]/"%s/"Detail: %s", 
                pSoap->version ? "SOAP 1." :/
            "Error ", pSoap->version ? (int)pSoap->version : pSoap->error, 
                pFaultCode, /
            pFaultSubCode ? pFaultSubCode : "no subcode", 
                pFalutString ? pFalutString : "[no reason]", /
            iFaultCode && *iFaultCode ? *iFaultCode : "[no detail]");
        }
    }
    return sError;
}

String2Currency, Currency2String

This is a useful function to convert currency to printable string and vice versa:

Collapse
bool CMyCurrencyConvertor::String2Currency(CString sCurrencyCode,
     ns1__Currency &oCurrencyCode)
{
    int iRet = soap_s2ns1__Currency(m_pCurrencyConvertor->soap,
        sCurrencyCode, &oCurrencyCode);

    switch(iRet)
    {
    case SOAP_OK :
        {            
            return true;
        }
            /*
        case SOAP_CLI_FAULT            
        case SOAP_SVR_FAULT            
        case SOAP_TAG_MISMATCH        
        case SOAP_TYPE            
        case SOAP_SYNTAX_ERROR        
        case SOAP_NO_TAG            
        case SOAP_IOB            
        case SOAP_MUSTUNDERSTAND        
        ...
        */
    default:
        {
            Log(GetSoapError());              
        }
    }    
    return false;
}

bool CMyCurrencyConvertor::Currency2String(ns1__Currency oCurrencyCode,
     CString &sCurrencyCode)
{
    const char* sName = 
        soap_ns1__Currency2s(m_pCurrencyConvertor->soap,oCurrencyCode);
    if (sName != NULL)
    {
        CString sTMP(sName);
        sCurrencyCode = sTMP;
        return true;
    }
    else
        Log(GetSoapError());                          
    return false;
}

Using the code in dialog

I created a dialog project and I use the methods I describe again.

I take a cycle for retrieval, and display the currency code, like this:

CString sCodeName;
    for (int iDx=ns1__Currency__AFA; iDx <;ns1__Currency__TRY+1; iDx++)
    {
        m_oMyCurrencyConvertor.Currency2String((ns1__Currency) iDx,
            sCodeName);

        m_cboFromCurrency.AddString(sCodeName);    
        m_cboToCurrency.AddString(sCodeName);    
    }    

When you push a button I retrieve the currency codes selected and use GetConversionRate to call WS.

    double dRate;
    CString sFromCurrency,sToCurrency;

    m_cboFromCurrency.GetWindowText(sFromCurrency);
    m_cboToCurrency.GetWindowText(sToCurrency);

    if (m_oMyCurrencyConvertor.GetConversionRate(sFromCurrency,
        sToCurrency,dRate))
    {
        int iColumnCount = m_lstConversionRate.GetItemCount();
        CString sRate;
        sRate.Format("%f",dRate);
        m_lstConversionRate.InsertItem(iColumnCount,sFromCurrency);        
        m_lstConversionRate.SetItemText(iColumnCount,iIDX_TO,sToCurrency);
        m_lstConversionRate.SetItemText(iColumnCount,iIDX_CONVERSION,sRate);        
    }    

Use it however you wish.

History

  • 12/07/2006: First release
<script src="/script/togglePre.js" type="text/javascript"></script>

About Dr.Luiji


Bertoneri Luigi, alias Dr.Luiji
Bachelor of Science in Computer science, year 2000 - University of Pisa (Italy).
I currently live in Italy.
Music I listen to: Slipknot, Type O Negative, Tool, Mushroomhead, Dry Kill Logic, System of a Down, White Zombie, RA, Slayer.

Click here to view Dr.Luiji's online profile.

 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用gSOAP与WebService - 第二部分 开发第一个WebService客户端(C++) 的相关文章

随机推荐

  • FormData的详解

    转自 https blog csdn net yezitoo article details 78339201 FormData 1 概述 FormData类型其实是在XMLHttpRequest 2级定义的 它是为序列化表以及创建与表单格
  • 三相桥式全控整流电路

    三相桥式全控整流电路原理图 原理图 阴极连接在一起的3个晶闸管 VT1 VT3 VT5 称为共阴极组 阳极连接在一起的3个晶闸管 VT4 VT6 VT2 称为共阳极组 共阴极组中与a b c三相电源相接的3个晶闸管分别为VT1 VT3 VT
  • Swift 枚举与结构体

    Swift 枚举 枚举简单的说也是一种数据类型 只不过是这种数据类型只包含自定义的特定数据 它是一组有共同特性的数据的集合 Swift 的枚举类似于 Objective C 和 C 的结构 枚举的功能为 它声明在类中 可以通过实例化类来访问
  • PyTorch中实现数据集的自定义读取

    一 创作缘由 数据集呈现的方式有很多种 今天和大家仔细谈一谈当我们要读取的数据集信息存储在文本文件时 我们如何读取数据集 最近在实现一个垃圾分类的任务 数据集中每张图片的名称和数据标签都记录在了文本文件中 垃圾分类数据集介绍 一共有6种不同
  • mybatis3 mysql自动生成主键_mybatis自动生成主键

    mysql 使用useGeneratedKey属性 INSERT INTO STUDENTS NAME EMAIL PHONE VALUES name email phone 其他的方式 使用selectKey子标签 属性 order be
  • [优化篇]OpenStack的Cinder后端存储技术——GlusterFS(1)

    题记 上一篇已经介绍了OpenStack的Cinder后端使用NFS存储技术 为什么要使用NFS呢 一般情况下 如果你的网络是千兆网络 在如果你考虑性能的要求 存储设置到宿主机本地硬盘效率会更好 例如我们可以在计算节点上安装cinder v
  • Javascript里有个C:Part 3 - 深入对象

    http cnodejs org topic 4f16442ccae1f4aa270010bd em Javascript里有个C em 系列文章 br ol br li a title 编辑 Javascript里有个C Part hre
  • 利用轨迹拼接分析实时可达区域

    如何快速得知从你的位置开始出发 在当前的交通状况下 5分钟之内能够抵达的空间区域范围 当你掏出手机打车时 出租车调度平台应该通知哪些范围的车主进行接单 前言 本篇介绍的是被国际著名数据库和数据挖掘会议DASFAA 2020 CCF B类 成
  • 基于ssm的高校学生课堂考勤系统的设计与实现

    研究的背景 目的和意义 1 研究背景 信息技术的迅猛发展 已经引起社会的深刻变革 信息时代的到来 迫切要求我们的学校管理进行变革 因此 信息化的考勤系统就在这种情况下变的越来越受欢迎 它给教师带来了更加高效处理考勤的方法 使得教学能够比较顺
  • 第三方软件测试z5x电池,三款手游开黑一天不充电 vivo Z5x续航测试

    随着全面屏手机的全面流行 手机的屏幕尺寸越来越大 屏幕尺寸和性能的提高 让续航问题逐渐成为消费者心中的疙瘩 从之前非智能机的两天一充 三天一充 到如今的一天一充 甚至一天两充 如何保持手机随时有电 成了一个难题 全面屏设计的vivo Z5x
  • link标签的其他作用

    在html中link标签的最常用 主要作用是进行css外部样式的引用 还有其他功能是引入icon 也就是可以用来设置这个网站的图标 另一条平时不太容易接触的功能 就是DNS预解析 属于前端优化的范畴 可以加快网页打开 响应的速度 因为每一次
  • apex您所在的地区目前不提供此物品_apex混合精度加速

    Pytorch 简介 Nvidia提供了一个混合精度工具apex 可以加速pytorch的训练效率 空间和时间上 号称可以这不降低模型性能的情况下 将训练速度提升2 4倍 训练显存开销减少为原来的一半 开源地址如下 https github
  • (一)低功耗设计目的与功耗的类型

    一 低功耗设计的目的 1 便携性设备等需求 电子产品在我们生活中扮演了极其重要的作用 便携性的电子设备便是其中一种 便携性设备需要电池供电 需要消耗电池的能量 在同等电能提供下 低功耗设计的产品就能够工作更长的时间 时间的就是生命 因此低功
  • Java怎样写优秀的代码_写优质Java代码的4个技巧

    咱们平时的编程使命不外乎便是将相同的技能套件应用到不同的项目中去 关于大多数状况来说 这些技能都是能够满意方针的 然而 有的项目或许需求用到一些特别的技能 因而工程师们得深入研究 去寻觅那些最简略但最有用的办法 在前一篇文章中 咱们讨论了必
  • springmvc项目搭建

    第3天 Spring SpringMVC MyBatis集成 学习目标 SSM集成 集成流程理解 集成SpringMVC 集成Spring 集成MyBatis 事务测试 第1章 搭建环境 1 1 整合流程 整合说明 SSM整合可以使用多种方
  • 【论文精读】Hierarchical Text-Conditional Image Generation with CLIP Latents

    Hierarchical Text Conditional Image Generation with CLIP Latents 前言 Abstract 1 Introduction 2 Method 2 1 Decoder 2 2 Pri
  • 怎样将自己的电脑变成一个服务器(本地服务器)

    你想将自己的电脑变成一个服务器吗 或许你还不知道我们自己的电脑也可摇身一变成为服务器 现在我分享一下把我们的电脑变为服务器的方法 工具 原料 电脑 步骤1 打开 修改电脑相关服务功能 1 1 点击 开始 打开 控制面板 2 2 打开 程序
  • 登录工程一:传统 Web 应用中的身份验证技术

    标题中的 传统Web应用 这一说法并没有什么官方定义 只是为了与 现代化Web应用 做比较而自拟的一个概念 所谓 现代化Web应用 指的是那些基于分布式架构思想设计的 面向多个端提供稳定可靠的高可用服务 并且在需要时能够横向扩展的Web应用
  • 华为OD机试 - 比较两个版本号的大小(Java)

    题目描述 输入两个版本号 version1 和 version2 每个版本号由多个子版本号组成 子版本号之间由 隔开 由大小写字母 数字组成 并且至少有一个字符 按从左到右的顺序比较子版本号 比较规则如下 子版本号前面的0不参与比较 比如
  • 使用gSOAP与WebService - 第二部分 开发第一个WebService客户端(C++)

    CurrencyConvertor How use gSOAP and WebServices Part 2 Doing the first WS client Download Demo Project 42 1 KB Download