来源:远方网络 | 2006-2-28 | (有2505人读过)
使用高层API发送和接收SOAP消息 在这个简单的例子中,服务器提供简单的算术服务:加法,减法,显示字符串(仅仅把输入的字符串回显出来)。为了创建这个应用程序,你需要在服务器和客户端创建不同的组件。在服务器这一端,你需要: 创建一个ActiveX DLL 创建一个WSDL文件列出服务以及服务中的操作 创建一个WSML文件,把WSDL中描述的服务的操作对应到COM对象的方法 创建一个ASP文件,处理SOAP请求 在客户端,你需要: 写一个VBScript调用服务器提供的操作 在客户端和服务器端,这个简单的应用使用了高层API,例如 SoapClient 和 SoapServer 来发送和接受消息。 在服务器端做以下工作 创建一个ActiveX DLL 打开VB,创建一个ActiveX DLL工程(注意,这个服务器上要安装IIS) 把工程名改为DocSample1,类名改为Sample1 在Sample1类中,添加以下代码: Public Function EchoString(ByVal testString As String) _ As String EchoString = testString End Function Public Function AddNumbers(ByVal NumberOne As Double, _ ByVal NumberTwo As Double) _ As Double AddNumbers = NumberOne + NumberTwo End Function Public Function SubtractNumbers(ByVal NumberOne As Double, _ ByVal NumberTwo As Double) _ As Double SubtractNumbers = NumberOne - NumberTwo End Function 选择菜单项 Make DocSample1.dll创建DLL文件 创建虚拟路径 在IIS环境下,创建一个Web虚拟路径,叫DocSample1 创建WSDL文件 在运行IIS的服务器上,把下面的文件DocSample1.wsdl复制到刚才建立的DocSample1路径下 DocSample1.wsdl文件: <?xml version=’1.0’ encoding=’UTF-16’ ?> <definitions name =’DocSample1’ targetNamespace = ’http://localhost/DocSample1/DocSample1.wsdl’ xmlns:tns=’http://localhost/DocSample1/DocSample1.wsdl’ xmlns:xsd1=’http://localhost/DocSample1/DocSample1.xsd’ xmlns:soap=’http://schemas.xmlsoap.org/wsdl/soap/’ xmlns=’http://schemas.xmlsoap.org/wsdl/’> <types> <schema targetNamespace= ’http://localhost/DocSample1/DocSample1.xsd’ xmlns=’http://www.w3.org/1999/XMLSchema’> </schema> </types> <message name=’EchoString’> <part name=’testString’ type=’string’/> </message> <message name=’EchoStringResponse’> <part name=’Result’ type=’string’/> </message> <message name=’AddNumbers’> <part name=’NumberOne’ type=’double’/> <part name=’NumberTwo’ type=’double’/> </message> <message name=’AddNumbersResponse’> <part name=’Result’ type=’double’/> </message> <message name=’SubtractNumbers’> <part name=’NumberOne’ type=’double’/> <part name=’NumberTwo’ type=’double’/> </message> <message name=’SubtractNumbersResponse’> <part name=’Result’ type=’double’/> </message> <portType name=’DocSample1PortType’> <operation name=’EchoString’ parameterOrder=’EchoStringInput1’> <input message=’tns:EchoString’ /> <output message=’tns:EchoStringResponse’ /> </operation> <operation name=’AddNumbers’ parameterOrder=’AddNumbersInput1 AddNumbersInput2’> <input message=’tns:AddNumbers’ /> <output message=’tns:AddNumbersResponse’ /> </operation> <operation name=’SubtractNumbers’ parameterOrder=’SubtractNumbersInput1 SubtractNumbersInput2’> <input message=’tns:SubtractNumbers’ /> <output message=’tns:SubtractNumbersResponse’ /> </operation> </portType> <binding name=’DocSample1Binding’ type=’tns:DocSample1PortType’ > <soap:binding style=’rpc’ transport=’http://schemas.xmlsoap.org/soap/http’ /> <operation name=’EchoString’ > <soap:operation soapAction=’http://localhost/DocSample1/DocSample1.asp’ /> <input> <soap:body use=’encoded’ namespace=’http://localhost/DocSample1/DocSample1.xsd’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’ /> </input> <output> <soap:body use=’encoded’ namespace=’http://localhost/DocSample1/DocSample1.xsd’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’ /> </output> </operation> <operation name=’AddNumbers’ > <soap:operation soapAction=’http://localhost/DocSample1/DocSample1.asp’ /> <input> <soap:body use=’encoded’ namespace=’http://localhost/DocSample1/DocSample1.xsd’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’ /> </input> <output> <soap:body use=’encoded’ namespace=’http://localhost/DocSample1/DocSample1.xsd’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’ /> </output> </operation> <operation name=’SubtractNumbers’ > <soap:operation soapAction=’http://localhost/DocSample1/DocSample1.asp’ /> <input> <soap:body use=’encoded’ namespace=’http://localhost/DocSample1/DocSample1.xsd’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’ /> </input> <output> <soap:body use=’encoded’ namespace=’http://localhost/DocSample1/DocSample1.xsd’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’ /> </output> </operation> </binding> <service name=’DocSample1’ > <port name=’DocSample1PortType’ binding=’tns:DocSample1Binding’ > <soap:address location=’http://localhost/DocSample1/DocSample1.asp’ /> </port> </service> </definitions>
创建WSML文件 在运行IIS的服务器上,把下面的文件DocSample1.wsml复制到刚才建立的DocSample1路径下 DocSample1.wsml文件: <?xml version=’1.0’ encoding=’UTF-16’ ?> <!-- Generated 12/14/00 by Microsoft SOAP SDK WSDL File Generator, Version 1.0 --> <servicemapping name=’DocSample1’> <service name=’DocSample1’> <using PROGID=’DocSample1.Sample1’ cachable=’0’ ID=’DocSample1Object’ /> <port name=’DocSample1PortType’> <operation name=’EchoString’> <execute uses=’DocSample1Object’ method=’EchoString’dispID=’1610809344’ > <parameter callIndex=’1’ name=’testString’ elementName=’testString’ /> <parameter callIndex=’-1’ name=’retval’ elementName=’Result’ /> </execute> </operation> <operation name=’AddNumbers’> <execute uses=’DocSample1Object’ method=’AddNumbers’ dispID=’1610809345’> <parameter callIndex=’1’ name=’NumberOne’ elementName=’NumberOne’ /> <parameter callIndex=’2’ name=’NumberTwo’ elementName=’NumberTwo’ /> <parameter callIndex=’-1’ name=’retval’ elementName=’Result’ /> </execute> </operation> <operation name=’SubtractNumbers’> <execute uses=’DocSample1Object’ method=’SubtractNumbers’ dispID=’1610809346’> <parameter callIndex=’1’ name=’NumberOne’ elementName=’NumberOne’ /> <parameter callIndex=’2’ name=’NumberTwo’ elementName=’NumberTwo’ /> <parameter callIndex=’-1’ name=’retval’ elementName=’Result’ /> </execute> </operation> </port> </service> </servicemapping> 把文件中ProgID改为你刚才创建的ActiveX DLL的ProgID(如果严格按步骤做,这个例子是不用改的) 创建ASP应用 在运行IIS的服务器上,把下面的文件DocSample1.asp复制到刚才建立的DocSample1路径下 <%@ LANGUAGE = VBScript %> <% Response.ContentType = "text/xml" %> <% set soapserver = CreateObject("MSSOAP.SoapServer") wsdl = Server.MapPath("DocSample1.wsdl") wsml = Server.MapPath("DocSample1.wsml") call soapserver.init(wsdl, wsml) call soapserver.SoapInvoke(request, response) %> 在客户端做以下工作 创建一个VBScript应用 在客户端,创建一个文件,包含以下代码 set soapclient = CreateObject("MSSOAP.SoapClient") Call soapclient.mssoapinit("http://localhost/DocSample1/DocSample1.wsdl", "DocSample1", "DocSample1PortType") wscript.echo soapclient.EchoString("Bob") wscript.echo soapclient.AddNumbers(2,3) wscript.echo soapclient.SubtractNumbers(3,2) On Error Resume Next wscript.echo soapclient.faultcode wscript.echo soapclient.faultstring wscript.echo soapclient.faultactor wscript.echo soapclient.detail 保存文件为clientDocSample1.vbs. 测试 打开命令行窗口,输入cscript clientDocSample1.vbs 注: 1.cscript是一个在命令行方式下运行script文件的东东,一般格式如下: cscript [//host option] scriptfile host option以"//"开头,设置一些参数 如 cscript //nologo XXX.vbs或者cscript //logo XXX.vbs,后面一个多了一行“版权所有”之类的话 2.报读WSDL文件错误时,试试将<?xml version=’1.0’ encoding=’UTF-16’ ?>改为<?xml version=’1.0’ encoding=’gb2312’ ?> 3.在Advanced Server下面,要注意DLL的执行权限 理解服务器端的开发 在这个简单的应用中,你首先在服务器端创建创建DocSample1.dll。这个DLL实现简单的算术操作(EchoString, AddNumbers,和SubtractNumbers)。注意,如果没有在运行IIS的服务器上创建这个DLL,必须将之复制到服务器上,并注册。 DocSample1.wsdl:这个WSDL文件描述了服务以及服务中包含的操作: EchoString, AddNumber,和 SubtractNumbers. 它定义了客户发送SOAP消息时必须遵从的格式。 DocSample1.wsml:这个WSML文件是Microsoft SOAP实现中特定的文件。 DocSample1.asp: 这个ASP文件处理SOAP请求。
|