. NET Web Services and Classic ASP

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

ITPro Today

June 1, 2004

6 Min Read
ITPro Today logo in a gray background | ITPro Today

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" %>

Imports System.Web.Services

Public Class Calculator: Inherits WebService

  Public Function Add(Num1 as integer,Num2 as integer) As Integer

       return Num1 + Num2

End Function

Public Function Substract(Num1 as integer,Num2 as integer) As Integer

       return Num1 - Num2

End Function

Public Function  Divide(Num1 as integer,Num2 as integer) As Integer

       return Num1 / Num2

End Function

End class

 

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

   Dim sRequest

   sRequest = "" & _

   " " & _  "     instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""    xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> " & _  "  " & _  "    " & _  "      2 " & _  "      3 " & _  "    " & _  "  " & _  " "    oXMLHTTP.setRequestHeader "Content-Type", "text/xml"  oXMLHTTP.setRequestHeader "SOAPAction", "http://tempuri.org/Add"  oXMLHTTP.send sRequest   Response.Write oXMLHTTP.responseText%>

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 !!!

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like