It’s quite easy to call a web service from an ASP.NET client. Let’s see how it can be done using classic ASP. Here is the Web Service code. We have created a Calculator web service, which gives the basic functionality of Add, Subtract and Divide. We'll call the Add method through a ASP page.
<%@ WebService Language ="Vb" Class="Calculator" %>
Public Class Calculator: Inherits WebService
<WebMethod()> Public Function Add(Num1 as integer,Num2 as integer) As Integer
return Num1 + Num2
<WebMethod()> Public Function Substract(Num1 as integer,Num2 as integer) As Integer
return Num1 - Num2
<WebMethod()> Public Function Divide(Num1 as integer,Num2 as integer) As Integer
return Num1 / Num2
Copy the above code and deploy it in your IIS web server. We’ll now see how we can call this web service via ASP.
Given below is the ASP code
' Call the web service to get an XML document
Set oXMLHTTP = Server.CreateObject("MSXML2.XMLHTTP")
'Create a link to the Web Service, replace the URL with your
'Web Service URL
oXMLHTTP.open "POST", "http://localhost/images/test.asmx",False
sRequest = "" & _
"<?xml version=""1.0"" encoding=""utf-8""?> " & _
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> " & _
" <soap:Body> " & _
" <Add xmlns=""http://tempuri.org/""> " & _
" <Num1>2</Num1> " & _
" <Num2>3</Num2> " & _
" </Add> " & _
" </soap:Body> " & _
oXMLHTTP.setRequestHeader "Content-Type", "text/xml"
oXMLHTTP.setRequestHeader "SOAPAction", "http://tempuri.org/Add"
We create a sRequest variable which has the SOAP request. This request is similar to one, which can be seen on the web service for the Add web method. AS can be seen, we have passed 2 and 3 as the parameters. Requesting this ASP page through the browser, we can see the output as 5, which can be seen.
Actually, by getting the View Source shows the entire SOAP response from the web service. This SOAP response has to be parsed so we can use the output effectively.
The parsing of the response is not shown above to keep the code simple.
Happy Coding !!!