下面是個控制臺的樣例
Toolkit3.0 終于給出VC6的樣例了,1.0只能看到VB和ASP的
#include <stdio.h>
#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; //你機器得安裝SOAP Toolkit3.0 ,1.0時,用using namespace時報錯
void Add()
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service.
Connector.CreateInstance(__uuidof(HttpConnector30)); //HttpConnector30 失敗,無法這樣創(chuàng)建Connector,CXX0017 Error :Symbol “HttpConnector30“ not found(搖頭、嘆氣!)
Connector->Property["EndPointURL"] = "http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl"; //這個當然得改成您自己的拉
Connector->Connect();
// Begin the message.
//Connector->Property["SoapAction"] = "uri:AddNumbers";
Connector->Property["SoapAction"] = "http://tempuri.org/DocSample1/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/DocSample1/message/","",""); //這是本地的Web Services,實際中要指定命名空間
Serializer->StartElement("NumberOne","","","");
Serializer->WriteString("5");
Serializer->EndElement();
Serializer->StartElement("NumberTwo","","","");
Serializer->WriteString("10");
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);
}
int main()
{
CoInitialize(NULL);
Add();
CoUninitialize();
return 0;
}
更改 EndPointURL 屬性的值. 在URL里指定你的服務器名.
OK
總結(jié)一下必要的關鍵步驟
1.導入類型庫
2.需要創(chuàng)建一個SoapConnector
3.下一步創(chuàng)建SoapSerializer
4.下一步把消息附加到SoapConnector的輸入流
5.下一步讀取結(jié)果.要讀取服務器的回復,客戶端應用需要使用SoapReader,
6.SoapReader被連接到SoapConnector輸出流
7.用IXMLDOMElement對象可以從SoapReader里讀到服務器的回復。