使用高層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請求。