How to consume web service in classic ASP application using Soap 3.0

It's fairly simple to consume web service using Microsoft Soap Type Library 3.0 in your classic ASP application.
Here I assume that you would have gone through my previous post "How to create ASP application using ASP Object Library" becasue here I will tell you what changes are needed to do in your ASPComp component that you would have created earlier.

So Open your ASPComp project and add following code in the clsASP class.
-------------------------------------
Public Sub Call_HelloWorld()

Dim oclient
Dim strg As String
On Error GoTo ErrorHandler
Set oclient = CreateObject("MSSOAP.SoapClient30")
oclient.MSSoapInit "http://localhost/WebService1/Service1.asmx?wsdl"

strg = oclient.HelloWorld("Sanjay")
oResponse.Write strg & vbCrLf

Exit Sub

ErrorHandler:

oResponse.Write "Invalid Web Service Request. Please check the Web Service URL: " & Err.Description

End Sub
--------------------------------------
Note:- In the above code web service in question is hosted on local machine and has a method named HelloWorld that takes string param and return string value but you have to replace this code with your web service and web method.

Now go to Project-> reference menu and add reference of Microsoft Soap Type Library 3.0

Generally it is installed with Office 2003 but if its not installed on your machine then you can download it from here and install it on your machine.

Now you will have to re-make ASPComp.dll

Note:- You will have to re-register the ASPComp.dll

After completing the component changes, open your asp page in the notepad and replace the method call with the newly created method.

So your MyASP.asp page will look like:

< %
Set oExample = Server.CreateObject("ASPComp.clsASP")
oExample.Call_HelloWorld
% >

You are done with the changes. So browse http://localhost/ASPTest/MyASP.asp in the IE window.

Reference: http://www.aspfree.com/c/a/VB.NET/Calling-a-Web-Service-using-VB6-with-SOAP-30/1/

No comments: