如果要用ASP来调用WevService,就一定要使用SOAP Toolkit或者XMLHTTP, 使用SOAP Client需要专门安装SOAP Toolkit,这对客户端来说不具有通用性,因此我们就学习使用XML来进行对WebService的调用。
例子一:
Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =Server.CreateObject("Microsoft.XMLDOM")
strWebserviceURL = "http://192.168.0.1/WebServiceTest/Service1.asmx/add"
'设置参数及其值
strRequest = "x="&request.Form("a")&"&y="&request.Form("b")
'strRequest = "tbname=123"
objHTTP.Open "POST", strWebserviceURL, False
'设置这个Content-Type很重要
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.Send(strRequest)
bOK = xmlDOC.load(objHTTP.responseXML)
'看看状态值
if objHTTP.Status=200 then
Set docXSL = server.CreateObject("Microsoft.XMLDOM")
docXSL.async = False
docXSL.load(server.MapPath("test1.xsl"))
'response.write docXSL.xml
Set docTran = server.CreateObject("Microsoft.XMLDOM")
xmlDOC.transformNodeToObject docXSL,docTran
'response.write docTran.xml
xmlStr = docTran.xml
xmlStr = Replace(xmlStr,"<","<",1,-1,1)
xmlStr = Replace(xmlStr,">",">",1,-1,1)
response.write xmlStr
end if
例子二:
关键你要知道找个服务有那些方法,如下面的Lookup,然后就是参数,
你可以构造xml文档,然后post到web 服务的asmx上面
sXml = ...
...
oXmlHttp.Send(sXml)
最后就可以得到它的返回,应该是xml的,采用dom去解析xml文档,得到你想要的结果.
<%
strDomain = Request.Form("strDomain")
strUrl = "http://whois.aspsir.com/whois.asmx/Lookup?strDomain=" & strDomain & ""
Set oXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
oXmlHttp.Open "GET", strUrl, False
oXmlHttp.Send()
If oXmlHttp.readyState <> 4 Then Response.Write Err.Description
strResult = bytes2BSTR(oXmlHttp.responseBody)
If Instr(strResult,"No match")<>0 Then
Response.Write "域名[www." & strDomain & "]居然还没有被注册!"
Else
Response.Write "呵呵,来晚了一步!域名已经被注册~"
End If
Set oXmlHttp = Nothing
Function bytes2BSTR(vIn)
Dim strReturn
Dim I, ThisCharCode, NextCharCode
strReturn = ""
For I = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn, I, 1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn, I + 1, 1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
I = I + 1
End If
Next
bytes2BSTR = strReturn
End Function
%>