Click here to Skip to main content
15,398,211 members
Articles / Web Development / HTML
Posted 14 Jun 2005


46 bookmarked

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


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:

<!--<span class="code-comment">#include virtual="/webservice.asp"--></span>
    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
    response.Write ws.response
    set ws = nothing

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:

option explicit
class WebService
  public Url
  public Method
  public Response
  public Parameters
  public function execute()
    dim xmlhttp
    Set xmlhttp = CreateObject("Microsoft.XMLHTTP") "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 & "&"
    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
  end function
  public sub Remove(nKey)
  end sub
  public function 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.


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


About the Author

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

QuestionTrouble with <!--#include virtual="/webservice.asp"--> Pin
rkear8923-Jan-13 1:48
Memberrkear8923-Jan-13 1:48 
GeneralMy vote of 2 Pin
Member 774276110-Mar-11 8:57
MemberMember 774276110-Mar-11 8:57 
GeneralMy vote of 1 Pin
Member 774276110-Mar-11 8:57
MemberMember 774276110-Mar-11 8:57 
Generalgerman Web service problem Pin
chsab42016-Jul-09 4:13
Memberchsab42016-Jul-09 4:13 
QuestionUse of HTTPS? Pin
Henrikop26-Jun-09 4:29
MemberHenrikop26-Jun-09 4:29 
AnswerRe: Use of HTTPS? Pin
asanoguera26-Jun-09 10:04
Memberasanoguera26-Jun-09 10:04 
Generali want to call dotnet webservice in my Asp page Pin
Member 406266014-Apr-09 15:24
MemberMember 406266014-Apr-09 15:24 
GeneralRe: i want to call dotnet webservice in my Asp page Pin
Member 424049315-Apr-09 3:57
MemberMember 424049315-Apr-09 3:57 
GeneralError when use FQDN Pin
DanAXCP30-May-08 5:06
MemberDanAXCP30-May-08 5:06 
AnswerRe: Error when use FQDN Pin
Member 424049330-May-08 5:20
MemberMember 424049330-May-08 5:20 
GeneralRe: Error when use FQDN Pin
DanAXCP30-May-08 11:37
MemberDanAXCP30-May-08 11:37 
GeneralComplex Arguments Pin
Scott.Jara28-May-08 8:34
MemberScott.Jara28-May-08 8:34 
AnswerRe: Complex Arguments Pin
Member 424049330-May-08 4:12
MemberMember 424049330-May-08 4:12 
QuestionGet value xmlhttp.responseText Pin
mpichiya29-Jun-07 16:15
Membermpichiya29-Jun-07 16:15 
General.NET version 1.1.4322.2032 vs 1.1.4322.2300 Pin
jafraley20-Apr-07 7:35
Memberjafraley20-Apr-07 7:35 
GeneralByte Array Pin
tknman070029-Mar-07 4:59
Membertknman070029-Mar-07 4:59 
GeneralGreat! Pin
AudunH25-Sep-06 19:43
MemberAudunH25-Sep-06 19:43 
GeneralError Type: msxml4.dll (0x80070005) Access is denied Pin
Olubisi Akintunde8-Dec-05 1:37
MemberOlubisi Akintunde8-Dec-05 1:37 
GeneralError Pin
YOONG CAME20-Oct-05 17:49
MemberYOONG CAME20-Oct-05 17:49 
GeneralRe: Error Pin
Antoni Sanoguera20-Oct-05 19:16
MemberAntoni Sanoguera20-Oct-05 19:16 
GeneralRe: Error Pin
YOONG CAME20-Oct-05 20:21
MemberYOONG CAME20-Oct-05 20:21 
GeneralRe: Error Pin
YOONG CAME20-Oct-05 23:53
MemberYOONG CAME20-Oct-05 23:53 
GeneralRe: Error Pin
Antoni Sanoguera8-Dec-05 2:04
MemberAntoni Sanoguera8-Dec-05 2:04 
GeneralThanks a million buddy. Pin
Wilbur J. Pereira13-Oct-05 5:58
MemberWilbur J. Pereira13-Oct-05 5:58 
GeneralRe: Thanks a million buddy. Pin
Antoni Sanoguera18-Oct-05 3:48
MemberAntoni Sanoguera18-Oct-05 3:48 

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.