ASP如何调用Delphi开发的WebService服务

先来看一下用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


 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值