VC6开发Web Services 客户端

下面是个控制台的样例
Toolkit3.0 终于给出VC6的样例了,1.0只能看到VB和ASP的

#include

 1<stdio.h>
 2
 3#import "msxml4.dll"   
 4using namespace MSXML2; 
 5
 6#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \   
 7exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \   
 8"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")   
 9using namespace MSSOAPLib30;  //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错 
10
11  
12void Add()   
13{   
14ISoapSerializerPtr Serializer;   
15ISoapReaderPtr Reader;   
16ISoapConnectorPtr Connector;   
17// Connect to the service.   
18Connector.CreateInstance(__uuidof(HttpConnector30));  **//HttpConnector30 失败,无法这样创建 Connector  ,CXX0017 Error :Symbol “HttpConnector30“ not found(摇头、叹气!)   
19** Connector-&gt;Property["EndPointURL"] = " http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl ";  //这个当然得改成您自己的拉   
20Connector-&gt;Connect(); 
21
22// Begin the message.   
23//Connector-&gt;Property["SoapAction"] = "uri:AddNumbers";   
24Connector-&gt;Property["SoapAction"] = " http://tempuri.org/DocSample1/action/Sample1.AddNumbers ";   
25Connector-&gt;BeginMessage(); 
26
27// Create the SoapSerializer object.   
28Serializer.CreateInstance(__uuidof(SoapSerializer30)); 
29
30// Connect the serializer object to the input stream of the connector object.   
31Serializer-&gt;Init(_variant_t((IUnknown*)Connector-&gt;InputStream)); 
32
33// Build the SOAP Message.   
34Serializer-&gt;StartEnvelope("","","");   
35Serializer-&gt;StartBody("");   
36Serializer-&gt;StartElement("AddNumbers"," http://tempuri.org/DocSample1/message/ ","",""); //这是本地的Web Services,实际中要指定命名空间   
37Serializer-&gt;StartElement("NumberOne","","","");   
38Serializer-&gt;WriteString("5");   
39Serializer-&gt;EndElement();   
40Serializer-&gt;StartElement("NumberTwo","","","");   
41Serializer-&gt;WriteString("10");   
42Serializer-&gt;EndElement();   
43Serializer-&gt;EndElement();   
44Serializer-&gt;EndBody();   
45Serializer-&gt;EndEnvelope();   
46  
47// Send the message to the XML Web service.   
48Connector-&gt;EndMessage(); 
49
50// Read the response.   
51Reader.CreateInstance(__uuidof(SoapReader30)); 
52
53// Connect the reader to the output stream of the connector object.   
54Reader-&gt;Load(_variant_t((IUnknown*)Connector-&gt;OutputStream), ""); 
55
56// Display the result.   
57printf("Answer: %s\n", (const char*)Reader-&gt;RpcResult-&gt;text);   
58  
59} 
60
61int main()   
62{   
63CoInitialize(NULL);   
64Add();   
65CoUninitialize();   
66return 0;   
67} 
68
69更改 EndPointURL 属性的值. 在URL里指定你的服务器名. 
70
71OK   
72  
73总结一下必要的关键步骤   
741.导入类型库 
75
762.需要创建一个SoapConnector 
77
783.下一步创建SoapSerializer 
79
804.下一步把消息附加到SoapConnector的输入流 
81
825.下一步读取结果.要读取服务器的回复,客户端应用需要使用SoapReader, 
83
846.SoapReader被连接到SoapConnector输出流 
85
867.用IXMLDOMElement对象可以从SoapReader里读到服务器的回复 
87
88附,SOAP ToolKit 3.0 下载地址: http://download.microsoft.com/download/xml/Install/3.0/W982KMeXP/EN-US/SoapToolkit30.EXE</stdio.h>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus