Click here to Skip to main content
14,145,240 members
Click here to Skip to main content
Add your own
alternative version

Stats

113.8K views
724 downloads
45 bookmarked
Posted 14 Jun 2005
Licenced CPOL

Class implementation for using webservices in ASP

, 14 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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:

<!--<span class="code-comment">#include virtual="/webservice.asp"--></span>
<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:

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

Share

About the Author

asanoguera
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

You may also be interested in...

Pro

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190518.1 | Last Updated 14 Jun 2005
Article Copyright 2005 by asanoguera
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid