HRESULT WSAccessor::Access(LPCTSTR sMethod, IWSParam* lpParams, LPCTSTR sSoapAction, LPCTSTR sRpcURI) { _sAnswer = ""; if (_sEndPointURL.size() == 0) { _sAnswer = "无效的目的地址!"; return -1; } string sAction = (sSoapAction[0] == 0x00 ? (_sNameSpace + sMethod) : (sSoapAction)); string sURI = (sRpcURI[0]==0x00 ? _sRpcURI : (sRpcURI)); HRESULT hr = 0; try { if( !m_bConnected) { /// 创建连接 // ISoapConnectorPtr Connector; if( m_pSoapConnector == NULL ) { //m_pSoapConnector = ISoapClientPtr(__uuidof(SoapClient30)); m_pSoapConnector.CreateInstance(__uuidof(HttpConnector30)); if( m_pSoapConnector == NULL ) return S_FALSE; } //hr = Connector.CreateInstance(__uuidof(HttpConnector30)); m_pSoapConnector->Property["EndPointURL"] = _sEndPointURL.c_str(); m_pSoapConnector->Property["SoapAction"] = sAction.c_str(); m_pSoapConnector->Property["Timeout"] = _variant_t(100L); hr = m_pSoapConnector->Connect(); if (S_OK != hr) { _sAnswer = "创建连接失败!"; return hr; } m_bConnected = TRUE; } /// 开始传送数据 hr = m_pSoapConnector->BeginMessage(); { /// 序列化数据 ISoapSerializerPtr Serializer; hr = Serializer.CreateInstance(__uuidof(SoapSerializer30)); hr = Serializer->Init(_variant_t((IUnknown*)m_pSoapConnector->InputStream)); hr = Serializer->StartEnvelope("", "", ""); { hr = Serializer->SoapNamespace("xsi", "http://www.w3.org/1999/XMLSchema-instance"); hr = Serializer->SoapNamespace("xsd", "http://www.w3.org/1999/XMLSchema"); hr = Serializer->StartBody("http://schemas.xmlsoap.org/soap/encoding/"); { /// 开始方法 hr = Serializer->StartElement(sMethod, sURI.c_str(), "", "ns1"); { /// 方法的参数 for (int i = 0; i < lpParams->GetSize(); i++) { TCHAR lpBuffer[4096] = {0}; lpParams->GetName(i, lpBuffer); hr = Serializer->StartElement(lpBuffer, "", "", ""); lpBuffer[0] = 0x00; lpParams->GetValue(i, lpBuffer); hr = Serializer->WriteString(lpBuffer); hr = Serializer->EndElement(); } } hr = Serializer->EndElement(); } hr = Serializer->EndBody(); } hr = Serializer->EndEnvelope(); } /// 结束数据 hr = m_pSoapConnector->EndMessage(); /// 读取反馈 ISoapReaderPtr Reader; Reader.CreateInstance(__uuidof(SoapReader30)); Reader->Load(_variant_t((IUnknown*)m_pSoapConnector->OutputStream), ""); if(Reader->Fault == NULL) { _sAnswer = (const char*)(Reader->RpcResult->text); return 0; } else { _sAnswer = (const char*)(Reader->Fault->text); return -1; } } catch(...) { _sAnswer = "发送数据时发生异常!"; return -1; } return 0; }
/** This function has been inspired by http://perfectxml.com/CPPSOAP.asp, Section 4 Another good source of information is http://www.codeguru.com/Cpp/COM-Tech/complus/soap/article.php/c3945/ Namespace should be: http://sqlct.tacc.utexas.edu */ unsigned char* vvClient::getRegionLowLevel(int lod, int x0, int y0, int x1, int y1, int slice0, int slice1, int dataset_id, const char* server, const char* ns) { unsigned char* ptr = NULL; #ifdef WIN32 #ifdef HAVE_SOAP char buf[32]; // for itoa HRESULT hr = S_OK; hr = CoInitialize(NULL); if (FAILED(hr)) { cerr << "Error: CoInitialize failed" << endl; return NULL; } ISoapSerializerPtr Serializer; ISoapReaderPtr Reader; ISoapConnectorPtr Connector; cerr << "Connector.CreateInstance" << endl; hr = Connector.CreateInstance(__uuidof(HttpConnector30)); cerr << "Connector->Property" << endl; Connector->Property[_T("EndPointURL")] = _T(server); cerr << "Connector->Connect" << endl; hr = Connector->Connect(); cerr << "Connector->Property" << endl; Connector->Property[_T("SoapAction")] = _T(server); cerr << "Connector->BeginMessage" << endl; hr = Connector->BeginMessage(); cerr << "Serializer.CreateInstance" << endl; hr = Serializer.CreateInstance(__uuidof(SoapSerializer30)); cerr << "Serializer->Init" << endl; hr = Serializer->Init(_variant_t((IUnknown*)Connector->InputStream)); cerr << "Serializer->StartEnvelope" << endl; hr = Serializer->StartEnvelope(_T(ns),_T("NONE"),_T("")); // Opening <body>: cerr << "Serializer->StartBody" << endl; hr = Serializer->StartBody(_T("")); // Create XML message: cerr << "Serializer->StartElement" << endl; hr = Serializer->StartElement(_T("GetRegion"), _T(""), _T("NONE"),_T("")); hr = Serializer->StartElement(_T("lod"), _T(""), _T("NONE"),_T("")); hr = Serializer->WriteString(itoa(lod, buf, 10)); hr = Serializer->EndElement(); hr = Serializer->StartElement(_T("x0"), _T(""), _T("NONE"),_T("")); hr = Serializer->WriteString(itoa(x0, buf, 10)); hr = Serializer->EndElement(); hr = Serializer->StartElement(_T("y0"), _T(""), _T("NONE"),_T("")); hr = Serializer->WriteString(itoa(y0, buf, 10)); hr = Serializer->EndElement(); hr = Serializer->StartElement(_T("x1"), _T(""), _T("NONE"),_T("")); hr = Serializer->WriteString(itoa(x1, buf, 10)); hr = Serializer->EndElement(); hr = Serializer->StartElement(_T("y1"), _T(""), _T("NONE"),_T("")); hr = Serializer->WriteString(itoa(y1, buf, 10)); hr = Serializer->EndElement(); hr = Serializer->StartElement(_T("slice0"), _T(""), _T("NONE"),_T("")); hr = Serializer->WriteString(itoa(slice0, buf, 10)); hr = Serializer->EndElement(); hr = Serializer->StartElement(_T("slice1"), _T(""), _T("NONE"),_T("")); hr = Serializer->WriteString(itoa(slice1, buf, 10)); hr = Serializer->EndElement(); hr = Serializer->StartElement(_T("dataset_id"), _T(""), _T("NONE"),_T("")); hr = Serializer->WriteString(itoa(dataset_id, buf, 10)); hr = Serializer->EndElement(); hr = Serializer->EndElement(); // </body> cerr << "Serializer->EndBody" << endl; hr = Serializer->EndBody(); cerr << "Serializer->EndEnvelope" << endl; hr = Serializer->EndEnvelope(); cerr << "Connector->EndMessage" << endl; try { hr = Connector->EndMessage(); if (FAILED(hr)) { cerr << "Error: Connector->EndMessage failed" << endl; return NULL; } } catch(...) { cerr << "Error in Connector->EndMessage" << endl; return NULL; } cerr << "Reader.CreateInstance" << endl; try { hr = Reader.CreateInstance(__uuidof(SoapReader30)); if (FAILED(hr)) { cerr << "Error: Reader.CreateInstance failed" << endl; return NULL; } } catch(...) { cerr << "Error in Reader.CreateInstance" << endl; return NULL; } cerr << "Reader->Load" << endl; try { hr = Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), _T("")); } catch (_com_error e) { cerr << "_com_error in Reader->Load: " << hr << " " << FAILED(hr) << endl; return NULL; } catch(...) { cerr << "Error in Reader->Load" << endl; return NULL; } // also try: printf("Answer: %s\n", (const char *)Reader->RPCResult->text); // and also MSXML2::IXMLDOMElementPtr ptr = Reader->RpcResult->get_xml((BSTR*)&bstrXml); cerr << "Reader->Dom" << endl; CComQIPtr<IXMLDOMDocument2> spResponseXMLDOM; spResponseXMLDOM = Reader->Dom; //TODO: Process the response SOAP XML and display the results //For now, just printing the response XML text as it is. USES_CONVERSION; cerr << "Response: " << (const char*)(W2A(spResponseXMLDOM->xml)) << endl; CoUninitialize(); #endif #else (void)lod; (void)x0; (void)y0; (void)x1; (void)y1; (void)slice0; (void)slice1; (void)dataset_id; (void)server; (void)ns; #endif return ptr; }