Click here to Skip to main content
15,888,600 members
Articles / Web Development / HTML
Article

Class implementation for using webservices in ASP

Rate me:
Please Sign up or sign in to vote.
3.73/5 (13 votes)
14 Jun 2005CPOL 131.5K   755   46   27
With that class you only have to set a few properties and you can access to the webservices, only need know the URL of webservice, the method to invoke, and parameters.

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:

VBScript
<!--#include virtual="/webservice.asp"-->
<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:

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Argentina Argentina
I born in 1977, I Studies computer science from age 12, at 18 he was working in a company that developed solutions for clinical and biochemical laboratories in QuickBasic and Btrieve, at 21 I started to work in a consulting firm that developed solutions for companies in QBX, VB5 and MSAccess, I also worked as a senior programmer for 5 years in a government agency analyzing and building management systems and statistics for decision making in VB6, ASP and MS-SQL 2000, independently developed several projects and also work for companies in the abroad in ASP / ASP.NET and MS-SQL 2000, I currently developing desktop applications and Web applications in MVC 3, Razor, Entity Framework, jQuery, VB.NET and C# and MS-SQL 2008

Comments and Discussions

 
Generalmsxml3.dll Error '80070005' Pin
NoradYeh22-Jun-05 15:35
NoradYeh22-Jun-05 15:35 
GeneralRe: msxml3.dll Error '80070005' Pin
asanoguera22-Jun-05 16:26
asanoguera22-Jun-05 16:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.