📄 sample.cpp
字号:
//*******************************************************************
// AUTHOR: Catalin Hatmanu
// PROJECT: vcrope
// DATE: 23.12.2000
// COMMENTS: -SOAP sample C++ client; contain
// methods based on wire transfer technique
// ( MS SOAP Toolkit required)
// Portions of code from Pedro Santos <psantos@INFORDESPORTO.PT>
//*******************************************************************
// Includes
// RopeTeste.cpp : Defines the entry point for the console application.
//
#include <atlbase.h>
#include <iostream>
#include <fstream>
//replace with the path of rope.dll
#import "F:\\Program Files\\SOAP_Toolkit\\ROPE\\rope.dll" named_guids raw_interfaces_only
#include <rope.h>
//MS demo server
CComBSTR bstrLocation = CComBSTR("http://www.soaptoolkit.com/soapdemo/services.xml");
//strings for preserving the service description and URI listener between calls
CComBSTR bstrSrvDesc, URI_LISTENER;
bool Connect()
{
long nSuccess, item = 0;
CComVariant varTemp;
ROPE::ISOAPPackagerPtr packer;
CComPtr<IUnknown> pIUnknown;
ROPE::IServiceDescriptorsPtr pIServiceDescriptors;
ROPE::ISDEndPointInfoPtr pISDEndPointInfo;
USES_CONVERSION;
try
{
HRESULT hr = packer.CreateInstance( ROPE::CLSID_SOAPPackager );
//load service description from this web service
hr = packer->LoadServicesDescription(ROPE::icURI,
bstrLocation, NULL, &nSuccess);
if( nSuccess != 1 )
throw E_FAIL;
//get service descriptors
hr = packer->get_GetServiceDescriptors(ROPE::icENDPOINTINFO, &varTemp);
pIUnknown.Attach( varTemp.pdispVal );
pIUnknown.QueryInterface( &pIServiceDescriptors );
//get first descriptor on the list
VariantInit(&varTemp);
hr = pIServiceDescriptors->get_Item(variant_t(item),&varTemp);
pIUnknown.Detach();
//get the endpoint(URI address) a.e. where the SOAP message will be sent
pIUnknown.Attach( varTemp.pdispVal );
hr = pIUnknown.QueryInterface( &pISDEndPointInfo );
hr = pISDEndPointInfo->get_URI(&URI_LISTENER);
//get the service description
hr = packer->get_ServicesDescription(&bstrSrvDesc);
}
catch(...)
{
std::cout << "Connection failure ! "<< std::endl;
return false;
}
std::cout << "Connection successfull "<< std::endl;
return true;
}
void GetStockMethod()
{
long nSuccess;
CComBSTR bstrRequestStruct, bstrRequestPayload, bstrResponsePayload;
//CComVariant varPrice;
ROPE::ISOAPPackagerPtr packer;
ROPE::IWireTransferPtr wireTrans;
USES_CONVERSION;
try
{
HRESULT hr = packer.CreateInstance( ROPE::CLSID_SOAPPackager );
//load service description using the string from previous Connect method call
hr = packer->LoadServicesDescription(ROPE::icSTRING,
bstrSrvDesc, NULL, &nSuccess);
if( nSuccess != 1 )
throw E_FAIL;
//seek in the service description
//the method structure a.e. what we need to call this method
hr = packer->GetMethodStruct(CComBSTR("GetStockQuote"), ROPE::icINPUT, &bstrRequestStruct);
// set method name
hr = packer->SetPayloadData(ROPE::icREQUEST, CComBSTR(""), CComBSTR("GetStockQuote"), bstrRequestStruct);
//set method parameters
hr = packer->SetParameter( ROPE::icREQUEST, CComBSTR("Symbol"), CComVariant("MSFT") );
hr = packer->SetParameter( ROPE::icREQUEST, CComBSTR("description"), CComVariant("any company") );
//get the data that will be sended to web server( payload )
hr = packer->GetPayload( ROPE::icREQUEST, &bstrRequestPayload);
std::cout << W2A(bstrRequestPayload) << std::endl << std::endl;
//post the payload
wireTrans.CreateInstance( ROPE::CLSID_WireTransfer);
hr = wireTrans->AddStdSOAPHeaders(URI_LISTENER,CComBSTR("GetStockQuote"),bstrRequestPayload.Length());
hr = wireTrans->PostDataToURI(URI_LISTENER, bstrRequestPayload, &bstrResponsePayload);
std::cout << W2A(bstrResponsePayload) << std::endl << std::endl;
//this is the response from service
hr = packer->SetPayload( ROPE::icRESPONSE, bstrResponsePayload);
//get return value
CComVariant varPrice;
hr = packer->GetParameter( ROPE::icRESPONSE, CComBSTR("result"), &varPrice);
std::cout << W2A(varPrice.bstrVal) << std::endl;
}
catch(...)
{
std::cout << "Something wrong happened !" << std::endl<< std::endl;
}
}
int main()
{
CoInitialize(NULL);
Connect();
GetStockMethod();
CoUninitialize();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -