Introduction
Recently, I tried to use for the first time a webservice from an ASP page and I really had problems, after hours I could access to my webservice from ASP with Microsoft.XMLHTTP, but the code was not very easy (if it is your first time), so I decided to package the implementation in a simple vbscript class with a few properties that allow to access to a webservice.
The page that use the vbscript class is:
<!---->
<html>
<head>
<title>testws</title>
</head>
<body>
<%
dim ws
set ws = new webservice
ws.url = "http://localhost/yourwebservice.asmx"
ws.method = "MethodName"
ws.parameters.Add "ParamName1",1
ws.parameters.Add "ParamName2",300
ws.parameters.Add "ParamNameN",500
ws.execute
response.Write ws.response
set ws = nothing
%>
</body>
</html>
Like you can see, it is very simple to use; define the properties, call the execute
method and the property response
will return the information from webservice.
The class that implement the call to webservice is:
<%
option explicit
class WebService
public Url
public Method
public Response
public Parameters
public function execute()
dim xmlhttp
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.open "POST", Url & "/" & Method, false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send Parameters.toString
response = xmlhttp.responseText
set xmlhttp = nothing
end function
Private Sub Class_Initialize()
Set Parameters = new wsParameters
End Sub
Private Sub Class_Terminate()
Set Parameters = Nothing
End Sub
End class
class wsParameters
public mCol
public function toString()
dim nItem
dim buffer
buffer = ""
for nItem = 1 to Count
buffer = buffer & Item(nItem).toString & "&"
next
if right(buffer,1)="&" then
buffer = left(buffer,len(buffer)-1)
end if
toString = buffer
end function
public sub Clear
set mcol = nothing
Set mCol = CreateObject("Scripting.Dictionary")
end sub
public sub Add(pKey,pValue)
dim newParameter
set newParameter = new wsParameter
newParameter.Key = pKey
newParameter.Value = pValue
mCol.Add mCol.count+1, newParameter
set newParameter = nothing
end sub
public function Item(nKey)
set Item=mCol.Item(nKey)
end function
public function ExistsXKey(pKey)
dim nItem
for nItem = 1 to mcol.count
if mCol.Item(nItem).key = pKey then
ExistsXKeyword = true
exit for
end if
next
end function
public sub Remove(nKey)
mCol.Remove(nKey)
end sub
public function Count()
Count=mCol.count
end function
Private Sub Class_Initialize()
Set mCol = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set mCol = Nothing
End Sub
end class
class wsParameter
public Key
public Value
public function toString()
toString = Key & "=" & Value
end function
end class
%>
I hope that this small code of my implementation can help you, thanks, and if any question please send me an email.