首先安装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