首页 > 其他分享 >VC MFC 使用 soap方式与web服务器交互

VC MFC 使用 soap方式与web服务器交互

时间:2023-03-15 15:58:02浏览次数:47  
标签:web VC WriteString Connector EndElement StartElement MFC bstr Serializer

首先安装Microsoft SOAP Toolkit 3.0    soapsdk.exe

链接:https://pan.baidu.com/s/15NeiuXQm4Wnvjvd8ngPwsA
提取码:l8yh

 

 包含头,引用库,命名空间

#import "msxml6.dll" rename_namespace("MSXML2")
#import "C:\Program Files (x86)\Common Files\MSSoap\Binaries\mssoap30.dll" \
    exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")

using namespace MSSOAPLib30;
    HRESULT hr;
    CString strResult;
    strResult.Empty();

    ISoapSerializerPtr Serializer;
    ISoapReaderPtr Reader;
    ISoapConnectorPtr Connector;//这几个东西用全局变量释放dll时崩溃,放到函数里面就好了
    hr = Connector.CreateInstance(__uuidof(HttpConnector30));
    if (FAILED(hr))
    {
        sprintf(cError, "false 9:初始化网络连接失败!");
        return cError;
    }

    Connector->Property["EndPointURL"] = strURL.GetBuffer(0);
    Connector->Property["Timeout"] = "10000";
    hr = Connector->Connect();
    if (FAILED(hr))
    {
        sprintf(cError, "false 9:服务器连接失败!");
        return cError;
    }

    Connector->Property["SoapAction"] = "http://tempuri.org/IProductQCDataCollect/UploadQCResult";
    Connector->BeginMessage();

    Serializer.CreateInstance(__uuidof(SoapSerializer30));
    Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
    Serializer->StartEnvelope("soap", _bstr_t(), _bstr_t());
    Serializer->SoapAttribute("xsd", _bstr_t(), "http://www.w3.org/2001/XMLSchema", "xmlns");
    Serializer->StartBody(_bstr_t());


    Serializer->StartElement(_bstr_t("UploadQCResult"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->SoapAttribute(_bstr_t("xmlns"), _bstr_t(), _bstr_t("http://tempuri.org/"), _bstr_t());
    Serializer->StartElement(_bstr_t("qcResult"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->StartElement(_bstr_t("ProductQCResult"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->SoapAttribute(_bstr_t("xmlns"), _bstr_t(), _bstr_t("http://schemas.datacontract.org/2004/07/AJ.MES.DataService"), _bstr_t());

    Serializer->StartElement(_bstr_t("ExtraInfo"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)("*"));
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("ProductSN"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)(strSN));
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("QCInspector"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)(strOP));
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("QCVersion"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)(strTPName));
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Results"), _bstr_t(), _bstr_t(), _bstr_t());

    Serializer->StartElement(_bstr_t("ProductQCItem"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->StartElement(_bstr_t("IsPass"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)("PASS"));
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("ItemName"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)("老化时间"));
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Low"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)("*"));
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Upper"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)("*"));
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Value"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strTime);
    Serializer->EndElement();
    Serializer->EndElement();

    Serializer->StartElement(_bstr_t("ProductQCItem"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->StartElement(_bstr_t("IsPass"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strAllResult);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("ItemName"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"电压");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Low"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strVoltMin);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Upper"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strVoltMax);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Value"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strVoltValue);
    Serializer->EndElement();
    Serializer->EndElement();

    Serializer->StartElement(_bstr_t("ProductQCItem"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->StartElement(_bstr_t("IsPass"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strAllResult);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("ItemName"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"电流");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Low"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strCurrMin);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Upper"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strCurrMax);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Value"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strCurrValue);
    Serializer->EndElement();
    Serializer->EndElement();

    Serializer->StartElement(_bstr_t("ProductQCItem"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->StartElement(_bstr_t("IsPass"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strAllResult);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("ItemName"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"功率");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Low"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strPowerMin);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Upper"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strPowerMax);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Value"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strPowerValue);
    Serializer->EndElement();
    Serializer->EndElement();

    Serializer->StartElement(_bstr_t("ProductQCItem"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->StartElement(_bstr_t("IsPass"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"PASS");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("ItemName"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"柜体温度");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Low"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"*");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Upper"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strTempSetValue);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Value"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strTempGetValue);
    Serializer->EndElement();
    Serializer->EndElement();

    Serializer->StartElement(_bstr_t("ProductQCItem"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->StartElement(_bstr_t("IsPass"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"PASS");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("ItemName"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"冷却液温度");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Low"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"*");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Upper"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strWaterTempSetValue);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Value"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strWaterTempGetValue);
    Serializer->EndElement();
    Serializer->EndElement();

    Serializer->StartElement(_bstr_t("ProductQCItem"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->StartElement(_bstr_t("IsPass"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"PASS");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("ItemName"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"冷却液流量");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Low"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)"*");
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Upper"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strWaterflowSetValue);
    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("Value"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strWaterflowGetValue);
    Serializer->EndElement();
    Serializer->EndElement();

    Serializer->EndElement();
    Serializer->StartElement(_bstr_t("WorkstationId"), _bstr_t(), _bstr_t(), _bstr_t());
    Serializer->WriteString((_bstr_t)strStation);
    Serializer->EndElement();
    Serializer->EndElement();
    Serializer->EndElement();
    Serializer->EndElement();
    Serializer->EndBody();
    Serializer->EndEnvelope();

    try
    {
        hr = Connector->EndMessage();
        if (FAILED(hr))
        {
            sprintf(cError, "false 9:数据上传发送失败!");
            return cError;
        }

        //这里没有返回值
        //Reader.CreateInstance(__uuidof(SoapReader30));
        //Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
        //temp = ((const char*)Reader->RpcResult->text);

    }
    catch (_com_error e)
    {
        sprintf(cError, "false 9:网络访问异常0!");
        return cError;
    }
    catch (CMemoryException* e)
    {
        sprintf(cError, "false 9:网络访问异常1!");
        return cError;
    }
    catch (CFileException* e)
    {
        sprintf(cError, "false 9:网络访问异常2!");
        return cError;
    }
    catch (CException* e)
    {
        sprintf(cError, "false 9:网络访问异常3!");
        return cError;
    }

对应的web服务器函数说明

 

 测试代码

void CMFCTestDlg::OnBnClickedBtOutput()
{
    // TODO: 在此添加控件通知处理程序代码
    CoInitialize(NULL);
    ISoapSerializerPtr Serializer;
    ISoapReaderPtr Reader;
    ISoapConnectorPtr Connector;
 
    HRESULT hr=Connector.CreateInstance(__uuidof(HttpConnector30)); //创建连接实例
    if(!SUCCEEDED(hr))
    {  
        MessageBox(_T("error"));
        return;
    }
    //服务所在地址和端口;
    Connector->Property["EndPointURL"] = "http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx?wsdl";//网上找到webservice网址
    Connector->Connect();
//    Connector->Property["SoapAction"] = "http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx?wsdl";
    Connector->BeginMessage();
    Serializer.CreateInstance(__uuidof(SoapSerializer30));
    Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
 
 
    Serializer->StartEnvelope("soap","","");//
    Serializer->SoapAttribute("xsi", "", "http://www.w3.org/2001/XMLSchema-instance", "xmlns");   
    Serializer->SoapAttribute("xsd", "", "http://www.w3.org/2001/XMLSchema", "xmlns");
//    Serializer->SoapAttribute("soap12", "", "http://schemas.xmlsoap.org/soap/envelope/", "xmlns");
    Serializer->SoapAttribute("soapenc","","http://schemas.xmlsoap.org/soap/encoding/","xmlns");
    Serializer->SoapAttribute("tns","","http://www.webxml.com.cn/","xmlns");
    Serializer->SoapAttribute("types","","http://www.webxml.com.cn/encodedTypes","xmlns");
    Serializer->StartBody(L"NONE");
 
    Serializer->StartElement("getEnCnTwoWayTranslator","http://WebXml.com.cn/","NONE","xmlns"); // 开放的接口
 
    Serializer->StartElement("Word","http://WebXml.com.cn/","NONE","");//参数
    Serializer->SoapAttribute("xsi:type","","xsd:string","");
    Serializer->WriteString("hello"); //传参
    Serializer->EndElement();
 
    Serializer->EndElement();
 
    Serializer->EndBody();
    Serializer->EndEnvelope();
 
    Connector->EndMessage(); 
 
    Reader.CreateInstance(__uuidof(SoapReader30));
 
    Reader->Load(_variant_t((IUnknown*)Connector->OutputStream),"");
 
    SetDlgItemText(IDC_EDIT1, Reader->RpcResult->xml);//(const char*)
 
}

 

标签:web,VC,WriteString,Connector,EndElement,StartElement,MFC,bstr,Serializer
From: https://www.cnblogs.com/ckrgd/p/17217994.html

相关文章

  • WEB前端开发-蚂蚁金服电话面试-React
        记录一下自己面试的感触,上周投了杭州的蚂蚁金服,然后第二天早上的时候,就收到了面试的邀请,晚上7点到9点等待面试,自己心里面还是慌慌的,当时自己在上班,下午一下班就......
  • WEB攻击模式库之反序列化学习总结
    1.1.序列化反序列化是什么(what) 序列化:对象序列化的最主要的用处就是在传递和保存对象的时候,保证对象的完整性和可传递性。序列化是把对象转换成有序字节流,以便在网络......
  • LoadRunner——web网页诊断图(菜单:Graphs)(十五)
    7.1、组成:Web页面诊断图页面组件细化图页面组件细化图(随时间)页面下载时间细化图页面下载时间细化图(随时间)第一次缓冲时间细化图第一次缓冲时间细化图(随时间)【web......
  • SpringMVC中的适配器模式
    目录一、适配器模式在SpringMVC框架应用的源码剖析二、模拟适配器的应用三种处理器适配器接口适配器对于三种不同处理器的适配CustomDispatchServlet测试三、分析适配器3.1......
  • 你需要知道的webpack高频面试题
    谈谈你对webpack的看法webpack是一个模块打包工具,可以使用它管理项目中的模块依赖,并编译输出模块所需的静态文件。它可以很好地管理、打包开发中所用到的HTML,CSS,JavaScr......
  • 一文彻底读懂webpack常用配置
    开发环境constwebpack=require("webpack");constpath=require('path')module.exports={//entry:{//a:'./src/0706/a.js',//c:'./......
  • TinyWebServer
    1.一文读懂社长的TinyWebServer为提高效率,我们将在这部分通过线程池来实现并发(多线程并发),为每个就绪的文件描述符分配一个逻辑单元(线程)来处理。Linux下有三种IO复用方式:e......
  • web测试 bug定位(待补充)
    1、前置条件1.1、需要的知识熟透系统业务、团队成员情况熟悉使用F12或抓包工具了解HTTP/HTTPS协议了解系统的架构,数据走向1.2、定位前的操作(1)保存bug产生的记录(2)排......
  • webRTC
    概念webRTC(webReal-Timecommunications)是一项实时通讯技术,是一个可以用在视频聊天、音频聊天或P2P文件分享等WebApp中的APIWebRTC主要由一下几个部分组成:getUserMe......
  • MFC-OutputDebugString调试输出
     ::OutputDebugString(str);输出到以下窗口:        ......