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

 

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

       return Num1 + Num2

End Function

 

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

       return Num1 - Num2

End Function

 

<WebMethod()> 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 = "" & _

   "<?xml version=""1.0"" encoding=""utf-8""?> " & _

  "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-  

  instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 

  xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> " & _

  "  <soap:Body> " & _

  "    <Add xmlns=""http://tempuri.org/""> " & _

  "      <Num1>2</Num1> " & _

  "      <Num2>3</Num2> " & _

  "    </Add> " & _

  "  </soap:Body> " & _

  "</soap:Envelope> "

 

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

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish