Click here to Skip to main content
Click here to Skip to main content

Using SOAP With Classic ASP / VBScript

, 28 Sep 2007
Rate this:
Please Sign up or sign in to vote.
How to use SOAP with Classic ASP / VBScript.

Introduction

This is an example of how to use SOAP in Classic ASP / VBScript to communicate with a Web Service. This article is intended for those who still have to work with legacy technologies at work (like myself). This script checks the query string for a user ID, updates an existing record if a user ID is present, and creates a new record otherwise.

Using the code

This is pretty straightforward stuff:

  1. Grab values from text inputs and assign them to variables.
  2. Concatenate your SOAP parameters and use type conversions where necessary.
  3. Put your SOAP parameters in your SOAP envelope.
  4. Then, send.
<script language="VBScript">
Function Return()
    Window.Navigate "https://www.realequityhomes.com/" & _ 
                    "rehadmin_manageprops.aspx"
End Function

Function AddProperty()
    Dim serviceUrl
    Dim TableHTML
    Dim SOAPRequest
    Dim SOAPParameters
    Dim SOAPResponse
    Dim oXmlHTTP
    Dim oXmlDOC
    Dim objNodeRecordList
    Dim objNodePropertyField
    Dim objRoot
    Dim bOK
    Dim iPointer
    Dim iLimit
    Dim strPropertyID
    Dim strPropertyType
    Dim strAgentID
    Dim strDescription
    Dim strStreet
    Dim strCity
    Dim strState
    Dim strZip
    Dim strPrice
    Dim strARV
    Dim strMV
    Dim strSQFT
    Dim strBuilt
    Dim strBedrooms
    Dim strBaths
    Dim strRepairs
    Dim strThumb
    Dim strImage
    Dim strEntered
    Dim strSold
    Dim strFeatured
    Dim strUnListed

    strThumb = "https://www.realequityhomes.com/" & _ 
               "_images/thumb_images/t_REHNoImage.jpg"
    strImage = "https://www.realequityhomes.com/" &_ 
               "_images/large_images/REHNoImage.jpg"
    strPropertyType = "2"
    strAgentID = "4"
    strDescription = ""
    strStreet = ""
    strCity = ""
    strState = ""
    strZip = ""
    strPrice = "0"
    strARV = "0"
    strMV = "0"
    strSQFT = "0"
    strBedrooms = ""
    strBaths = ""
    strBuilt = "0"
    strRepairs = ""
    strEntered = ""
    strSold = ""
    strFeatured = "0"
    strUnListed = "0"

    If document.getElementById("chkCommercial").checked Then
        strPropertyType = "1"
    If document.getElementById("chkFeatured").checked Then
        strFeatured = "1"
    If document.getElementById("chkUnListed").checked Then
        strUnListed = "1"

    strPropertyID = document.getElementById("txtPropertyID").value
    strDescription = document.getElementById("txtDescription").value
    strStreet = document.getElementById("txtStreet").value
    strCity = document.getElementById("txtCity").value
    strState = document.getElementById("txtState").value
    strZip = document.getElementById("txtZip").value
    strBedrooms = document.getElementById("txtBedrooms").value
    strBaths = document.getElementById("txtBaths").value
    strRepairs = document.getElementById("txtRepairs").value
    If document.getElementById("txtPrice").value <> "" Then
        strPrice = document.getElementById("txtPrice").value
    If document.getElementById("txtARV").value <> "" Then
        strARV = document.getElementById("txtARV").value
    If document.getElementById("txtMV").value <> "" Then
        strMV = document.getElementById("txtMV").value
    If document.getElementById("txtSQFT").value <> "" Then
        strSQFT = document.getElementById("txtSQFT").value
    If document.getElementById("txtBuilt").value <> "" Then
        strBuilt = document.getElementById("txtBuilt").value
    If document.getElementById("txtThumb").value <> "" Then
        strThumb = document.getElementById("txtThumb").value
    If document.getElementById("txtImage").value <> "" Then
        strImage = document.getElementById("txtImage").value
                
    iPointer = InStr(1, strRepairs, "&", 0)
    Do While iPointer > 0 And iLimit < 100
        strRepairs = Left(strRepairs, iPointer - 1) & _
                     "&amp;" & Mid(strRepairs, iPointer + 1)
        iPointer = InStr(iPointer + 5, strRepairs, "&", 0)
        iLimit = iLimit + 1
    Loop

    strEntered = document.getElementById("txtEntered").value
    strSold = document.getElementById("txtSold").value
                
    SOAPParameters = " <lPropertyType>" & _
                     CLng(strPropertyType) & "</lPropertyType>"
    SOAPParameters = SOAPParameters & " <lAgentID>" & _
                     CLng(strAgentID) & "</lAgentID>"
    SOAPParameters = SOAPParameters & " <sPropertyDateEntered>" & _
                     strEntered & "</sPropertyDateEntered>"
    SOAPParameters = SOAPParameters & " <sPropertyDateSold>" & _
                     strSold & "</sPropertyDateSold>"
    SOAPParameters = SOAPParameters & " <sPropertyDescription>" & _
                     strDescription & "</sPropertyDescription>"
    SOAPParameters = SOAPParameters & " <sPropertyRepairs>" & _
                     strRepairs & "</sPropertyRepairs>"
    SOAPParameters = SOAPParameters & " <sPropertyBedrooms>" & _
                     strBedrooms & "</sPropertyBedrooms>"
    SOAPParameters = SOAPParameters & " <sPropertyBaths>" & _
                     strBaths & "</sPropertyBaths>"
    SOAPParameters = SOAPParameters & " <lPropertySQFT>" & _
                     CLng(strSQFT) & "</lPropertySQFT>"
    SOAPParameters = SOAPParameters & " <iPropertyBuilt>" & _
                     CInt(strBuilt) & "</iPropertyBuilt>"
    SOAPParameters = SOAPParameters & " <lPropertyPrice>" & _
                     CLng(strPrice) & "</lPropertyPrice>"
    SOAPParameters = SOAPParameters & " <lPropertyARV>" & _
                     CLng(strARV) & "</lPropertyARV>"
    SOAPParameters = SOAPParameters & " <lPropertyMV>" & _
                     CLng(strMV) & "</lPropertyMV>"
    SOAPParameters = SOAPParameters & " <sPropertyAddressStreet>" & _
                     strStreet & "</sPropertyAddressStreet>"
    SOAPParameters = SOAPParameters & " <sPropertyAddressCity>" & _
                     strCity & "</sPropertyAddressCity>"
    SOAPParameters = SOAPParameters & " <sPropertyAddressState>" & _
                     strState & "</sPropertyAddressState>"
    SOAPParameters = SOAPParameters & " <sPropertyAddressZip>" & _
                     strZip & "</sPropertyAddressZip>"
    SOAPParameters = SOAPParameters & " <sPropertyThumbURL>" & _
                     strThumb & "</sPropertyThumbURL>"
    SOAPParameters = SOAPParameters & " <sPropertyImageURL>" & _
                     strImage & "</sPropertyImageURL>"
    SOAPParameters = SOAPParameters & " <iPropertyFeatured>" & _
                     CInt(strFeatured) & "</iPropertyFeatured>"
    SOAPParameters = SOAPParameters & " <iPropertyUnListed>" & _
                     CInt(strUnListed) & "</iPropertyUnListed>"

                
    serviceUrl = "https://www.realequityhomes.com/ECTWebServices" & _ 
                 "/REHPropertyServices.asmx"
    Set oXmlHTTP = CreateObject("Microsoft.XMLHTTP")
    oXmlHTTP.Open "POST", serviceUrl, False 

    If strPropertyID = "" Then
        oXmlHTTP.setRequestHeader "Content-Type", "text/xml; charset=utf-8" 
        oXmlHTTP.setRequestHeader "SOAPAction", _
                 "http://tempuri.org/ECTWebServices/" & _ 
                 "REHPropertyServices/AddProperty" 
        SOAPRequest = "<?xml version='1.0' encoding='utf-8'?> <soap:Envelope"
        SOAPRequest = SOAPRequest & " xmlns:xsi=""http://" & _ 
                      "www.w3.org/2001/XMLSchema-instance"""
        SOAPRequest = SOAPRequest & " xmlns:xsd=""http://www." & _ 
                      "w3.org/2001/XMLSchema"""
        SOAPRequest = SOAPRequest & " xmlns:soap=""http://schemas" & _ 
                      ".xmlsoap.org/soap/envelope/"">"
        SOAPRequest = SOAPRequest & " <soap:Body>"
        SOAPRequest = SOAPRequest & " <AddProperty xmlns=""http:" & _ 
                      "//tempuri.org/ECTWebServices/REHPropertyServices"">"
        SOAPRequest = SOAPRequest & SOAPParameters
        SOAPRequest = SOAPRequest & " </AddProperty>"
        SOAPRequest = SOAPRequest & " </soap:Body>"
        SOAPRequest = SOAPRequest & " </soap:Envelope>"
        'MsgBox("new")
        'MsgBox(SOAPParameters)
    Else
        oXmlHTTP.setRequestHeader "Content-Type", "text/xml; charset=utf-8" 
        oXmlHTTP.setRequestHeader "SOAPAction", _
                 "http://tempuri.org/ECTWebServices/" & _ 
                 "REHPropertyServices/UpdateProperty" 
        SOAPRequest = "<?xml version='1.0' encoding='utf-8'?> <soap:Envelope"
        SOAPRequest = SOAPRequest & _
                      " xmlns:xsi=""http://www.w3." & _ 
                      "org/2001/XMLSchema-instance"""
        SOAPRequest = SOAPRequest & " xmlns:xsd=""http://" & _ 
                      "www.w3.org/2001/XMLSchema"""
        SOAPRequest = SOAPRequest & " xmlns:soap=""http:" & _ 
                      "//schemas.xmlsoap.org/soap/envelope/"">"
        SOAPRequest = SOAPRequest & " <soap:Body>"
        SOAPRequest = SOAPRequest & " <UpdateProperty " & _ 
                      "xmlns=""http://tempuri.org/ECTWebServices/" & _ 
                      "REHPropertyServices"">"
        SOAPRequest = SOAPRequest & " <lPropertyID>" & _
                      CLng(strPropertyID) & "</lPropertyID>"
        SOAPRequest = SOAPRequest & SOAPParameters
        SOAPRequest = SOAPRequest & " </UpdateProperty>"
        SOAPRequest = SOAPRequest & " </soap:Body>"
        SOAPRequest = SOAPRequest & " </soap:Envelope>"
        'MsgBox("upload")
        'MsgBox(SOAPParameters)
    End If

    oXmlHTTP.send SOAPRequest 
    SOAPResponse = oXmlHTTP.responseXML.xml
    'MsgBox(SOAPResponse)
                
    Document.URL = "https://www.realequityhomes.com/" & _ 
                   "rehadmin_manageprops.aspx"
                
End Function    
</script>

License

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

Share

About the Author

AdamNThompson
Web Developer
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionWhy not use Soap Toolkit 3 PinmemberCelare2-Oct-07 9:21 
AnswerRe: Why not use Soap Toolkit 3 PinmemberAdamNThompson2-Oct-07 16:07 
QuestionRe: Why not use Soap Toolkit 3 PinmemberCelare4-Oct-07 5:52 
AnswerRe: Why not use Soap Toolkit 3 PinmemberAdamNThompson4-Oct-07 8:08 
AnswerRe: Why not use Soap Toolkit 3 PinmemberAdamNThompson7-Oct-07 7:31 
AnswerRe: Why not use Soap Toolkit 3 PinmemberCelare8-Oct-07 10:41 
Absolutely, in VS2005 you just right click and Add Web Reference. It's literally that easy.
 
If you're wanting to look into it further then I'd recommend the following Microsoft webcasts "Designing Distributed Applications Around Web Services" and "What’s New for ASP.NET Web Services (ASMX) in .NET 2.0" (pay attention to support for the new nullable types in parameters). Just google for them. If you want to go a stage further and separate out your data access tier then "Implementing a Data Access Layer with the Visual Studio 2005 Dataset Designer" is definitely worth watching too.
 
Remember though that Web Services are not the answer to everything. They have drawbacks too! All parameters must be serialisable as XML. If any object can't natively serialise to XML then you have to implement serialisation yourself. Plus all parameters are passed as XML which nearly always means plain text, so bear that in mind when monitoring network usage / bandwidth.
 
Finally, if you do need to access these web services from legacy unmanaged code (VB, ASP, etc) then you're still going to have to use the SOAP Toolkit but bear in mind that it doesn't support nullable types.
 
Good luck with your architectural shift. It sounds quite exciting and I'm sure you're going to get a kick out of tackling it head on!
AnswerRe: Why not use Soap Toolkit 3 PinmemberCelare8-Oct-07 22:30 
GeneralRe: Why not use Soap Toolkit 3 PinmemberAdamNThompson9-Oct-07 10:24 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140902.1 | Last Updated 28 Sep 2007
Article Copyright 2007 by AdamNThompson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid