Click here to Skip to main content
15,884,099 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.4K   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

 
QuestionTrouble with <!--#include virtual="/webservice.asp"--> Pin
rkear8923-Jan-13 1:48
rkear8923-Jan-13 1:48 
GeneralMy vote of 2 Pin
Member 774276110-Mar-11 8:57
Member 774276110-Mar-11 8:57 
GeneralMy vote of 1 Pin
Member 774276110-Mar-11 8:57
Member 774276110-Mar-11 8:57 
Generalgerman Web service problem Pin
chsab42016-Jul-09 4:13
chsab42016-Jul-09 4:13 
QuestionUse of HTTPS? Pin
Henrikop26-Jun-09 4:29
Henrikop26-Jun-09 4:29 
AnswerRe: Use of HTTPS? Pin
asanoguera26-Jun-09 10:04
asanoguera26-Jun-09 10:04 
Generali want to call dotnet webservice in my Asp page Pin
Member 406266014-Apr-09 15:24
Member 406266014-Apr-09 15:24 
GeneralRe: i want to call dotnet webservice in my Asp page Pin
Member 424049315-Apr-09 3:57
Member 424049315-Apr-09 3:57 
GeneralError when use FQDN Pin
DanAXCP30-May-08 5:06
DanAXCP30-May-08 5:06 
AnswerRe: Error when use FQDN Pin
Member 424049330-May-08 5:20
Member 424049330-May-08 5:20 
GeneralRe: Error when use FQDN Pin
DanAXCP30-May-08 11:37
DanAXCP30-May-08 11:37 
I created a web service within my intranet environment and I can access it through both

http://mywebserver/webservice.asmx
or
http://mywebserver.mydomain.com/webservice.asmx

And I use the Class implementation you provieded to do all the soap request and reponse. Here is what I have in my test.asp file

*******************************************



<title>TEST


]]>

*************************************
everything works fine when I use http://mywebserver/webservice.asmx as the url
however, when you change the ws.url to "http://mywebserver.mydomain.com/webservice.asmx" it gives me a error message on the page saying "Error Occurred: 401 - Unauthorized"

Also I have SSL setup on my webserver so when you use
ws.url = "https://mywebserver.mydomain.com/webservice.asmx" I get the following
"Bad Request (Invalid URL)"
GeneralComplex Arguments Pin
Scott.Jara28-May-08 8:34
Scott.Jara28-May-08 8:34 
AnswerRe: Complex Arguments Pin
Member 424049330-May-08 4:12
Member 424049330-May-08 4:12 
QuestionGet value xmlhttp.responseText Pin
mpichiya29-Jun-07 16:15
mpichiya29-Jun-07 16:15 
General.NET version 1.1.4322.2032 vs 1.1.4322.2300 Pin
jafraley20-Apr-07 7:35
jafraley20-Apr-07 7:35 
GeneralByte Array Pin
tknman070029-Mar-07 4:59
tknman070029-Mar-07 4:59 
GeneralGreat! Pin
AudunH25-Sep-06 19:43
AudunH25-Sep-06 19:43 
GeneralError Type: msxml4.dll (0x80070005) Access is denied Pin
Olubisi Akintunde8-Dec-05 1:37
Olubisi Akintunde8-Dec-05 1:37 
GeneralError Pin
YOONG CAME20-Oct-05 17:49
YOONG CAME20-Oct-05 17:49 
GeneralRe: Error Pin
Antoni Sanoguera20-Oct-05 19:16
Antoni Sanoguera20-Oct-05 19:16 
GeneralRe: Error Pin
YOONG CAME20-Oct-05 20:21
YOONG CAME20-Oct-05 20:21 
GeneralRe: Error Pin
YOONG CAME20-Oct-05 23:53
YOONG CAME20-Oct-05 23:53 
GeneralRe: Error Pin
Antoni Sanoguera8-Dec-05 2:04
Antoni Sanoguera8-Dec-05 2:04 
GeneralThanks a million buddy. Pin
Wilbur J. Pereira13-Oct-05 5:58
Wilbur J. Pereira13-Oct-05 5:58 
GeneralRe: Thanks a million buddy. Pin
Antoni Sanoguera18-Oct-05 3:48
Antoni 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.