VC连接soap

首先下载并安装SoapToolkit30,搭建IIS服务(baidu下吧)


建立vc控制台程序,内容如下:

把下面的192.168.120.59都替换成你的服务的ip,如果在本地搭建的改为localhost


#include
#import "msxml4.dll"
using namespace MSXML2;
#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30;
void Add()
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service.
Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = "http://192.168.120.59/DocSample1/DocSample1.wsdl";
Connector->Connect();

// Begin the message.
//Connector->Property["SoapAction"] = "uri:AddNumbers";
Connector->Property["SoapAction"] = "http://tempuri.org/action/Sample1.AddNumbers";
Connector->BeginMessage();

// Create the SoapSerializer object.
Serializer.CreateInstance(__uuidof(SoapSerializer30));

// Connect the serializer object to the input stream of the connector object.
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));

// Build the SOAP Message.
Serializer->StartEnvelope("","","");
Serializer->StartBody("");
Serializer->StartElement("AddNumbers","http://tempuri.org/message/","","");//AddNumbers是下面wsdl里的方法
Serializer->StartElement("NumberOne","","","");
Serializer->WriteString("5");//写入AddNumbers方法里的参数1
Serializer->EndElement();
Serializer->StartElement("NumberTwo","","","");
Serializer->WriteString("10");//写入AddNumbers方法里的参数2
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();

// Send the message to the XML Web service.
Connector->EndMessage();

// Read the response.
Reader.CreateInstance(__uuidof(SoapReader30));

// Connect the reader to the output stream of the connector object.
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");

// Display the result.
printf("Answer: %s\n", (const char*)Reader->RpcResult->text);//AddNumbers得到的是两个参数的加和

}

int main()
{
CoInitialize(NULL);
Add();
CoUninitialize();
return 0;
}


http://192.168.120.59/DocSample1/DocSample1.wsdl内容如下




你可能感兴趣的:(VC连接soap)