先來看一下用Delphi開發的WebService服務接口CzyLogin方法原型:
//判斷操作員是否登入成功,傳回值true/false,若登入失敗sError參數傳回失敗原因,其原型為:
function CzyLogin(const Czy_ID,Czy_Pwd:string;const sVersion:string;out sError:string):Boolean;stdcall;
begin
//...
end;
webservices.asp檔案:
<%
Dim WebSrv,WebSrvUrl,WebNameSpace
WebSrv="localhost:8081" '提供WebService服務的伺服器,形式:IP:Port
WebSrvUrl="http://"&WebSrv&"/NetPayWadSrv.NetPay/soap/IAdmin"
WebNameSpace="urn:uAdminIntf-IAdmin" '接口函數所在的命名空間
Function CzyLogin(Czy_ID,Czy_Pwd,sError)
'CzyLogin函數判斷操作員登入是否成功,其原型為:
'function CzyLogin(const Czy_ID,Czy_Pwd:string;const sVersion:string;out sError:string):Boolean;stdcall;
'//傳回值true/false,若登入失敗sError參數傳回失敗原因。
sVersion = "1.1.0.28"
SoapRequest="<?xml version='1.0'?>"&_
"<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' "&_
"xmlns:xsd='http://www.w3.org/2001/XMLSchema' "&_
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "&_
"xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'>"&_
"<SOAP-ENV:Body SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>"&_
"<NS1:CzyLogin xmlns:NS1='"&WebNameSpace&"'>"&_
"<Czy_ID xsi:type='xsd:string'>"&Czy_ID&"</Czy_ID>"&_
"<Czy_Pwd xsi:type='xsd:string'>"&Czy_Pwd&"</Czy_Pwd>"&_
"<sVersion xsi:type='xsd:string'>"&sVersion&"</sVersion>"&_
"</NS1:CzyLogin>"&_
"</SOAP-ENV:Body>"&_
"</SOAP-ENV:Envelope>"
'Response.Write(SoapRequest)
'Response.End()
Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",WebSrvUrl,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset='utf-8'"
xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "User-Agent", "Borland SOAP 1.2"
xmlhttp.setRequestHeader "HOST",WebSrv
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.setRequestHeader "SOAPAction", WebNameSpace&"#CzyLogin"
xmlhttp.setRequestHeader "Cache-Control", "no-cache"
xmlhttp.Send(SoapRequest)
'Response.Write xmlhttp.Status&"|"&xmlhttp.StatusText
'Response.Write xmlhttp.responsetext
'Response.End()
If xmlhttp.Status = 200 Then
Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
'=====================BEGIN 測試代碼===================='
'xmlDOC.Save("e:\\result.xml")
'xmlStr = xmlDOC.xml
'xmlStr = Replace(xmlStr,"<","<",1,-1,1)
'xmlStr = Replace(xmlStr,">",">",1,-1,1)
'Response.write xmlStr
'Response.write "<hr />"
'=====================END 測試代碼===================='
CzyLogin=xmlDOC.documentElement.selectNodes("//return")(0).text
sError=xmlDOC.documentElement.selectNodes("//sError")(0).Text
'selectNodes("//NS1:CzyLoginResponse")(0).Text等同于childNodes.item(0).Text
'均為傳回所有參數字元串相加的總串
'str=xmlDOC.documentElement.selectNodes("//NS1:CzyLoginResponse")(0).Text
'str=xmlDOC.documentElement.childNodes.item(0).Text
'Response.write CzyLogin&"<br />"
'Response.write sError&"<br />"
Set xmlDOC = nothing
Else
CzyLogin="false"
'Response.Write xmlhttp.Status&" "
sError=xmlhttp.StatusText
End if
Set xmlhttp = Nothing
End Function
%>
其中最關鍵是如何擷取WebService接口函數所在的命名空間,我們可以在Delphi中使用Web App Debug模式調試C/S用戶端的方法來擷取xml檔案格式和命名空間,如:

例如:
在<NS1:CzyLogin xmlns:NS1='urn:uAdminIntf-IAdmin'>
和xmlhttp.setRequestHeader "SOAPAction", "urn:uAdminIntf-IAdmin#CzyLogin"中,
uAdminIntf-IAdmin就是CzyLogin接口方法函數所在的命名空間,命名空間描述文本由“接口單元名-接口名”構成,接口描述文本由“命名空間#接口名稱”構成,如下圖:
Test.asp測試檔案:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage=65001%>
<%Response.charset="utf-8"%>
<!-- #include file="webservices.asp"-->
<%
Dim czyid,czypwd,sError
czyid = "admin"
czypwd = "admin123"
sError = ""
Response.Write "czyid='"&czyid&"'<br />"
Response.Write "czypwd='"&czypwd&"'<br />"
Response.Write "sError='"&sError&"'<br />"
Response.Write "執行<br />"
Response.Write "status=CzyLogin(czyid,czypwd,sError)"
Response.Write "<br />結果:<hr />"
status=CzyLogin(czyid,czypwd,sError)
Response.write "status="&status&"<br />"
If status="true" Then
Response.write "登入成功!"
else
Response.write "登入失敗!sError="&sError
End If
%>
運作結果:
下圖為登入失敗的執行結果:
下圖為登入成功的執行結果:
測試代碼下載下傳:http://download.csdn.net/detail/xieyunc/9764212