Windows Scripting
  Home arrow Windows Scripting arrow Page 5 - Handling Live Web Content in WSH - Part 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Handling Live Web Content in WSH - Part 2
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-12-19

    Table of Contents:
  • Handling Live Web Content in WSH - Part 2
  • Making the Connection
  • Handling the Response
  • Handling Errors and Exceptions
  • Implementing a Workaround
  • Other Uses for XMLHttpRequest

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Handling Live Web Content in WSH - Part 2 - Implementing a Workaround


    (Page 5 of 6 )

    By design, the XMLHttpRequest object has an event called onReadyStateChange for this.  However, it isn’t fully supported and doesn’t work with VBScript, so we’ll create our own workaround in the form of a  timeout loop after we call the Send() method.

    varCounter = 0

    Do Until (objxmlHTTP.ReadyState = 4) Or (varCounter = 30)

       varCounter = varCounter + 1

       Wscript.Sleep(1000)

    Loop

    We start by creating a counter variable.  Then we create a loop that increments our counter.  The loop will break out any time our ReadyState indicates that the connection is complete or when approximately 30 seconds have passed.  We use the Sleep() method to ensure that each iteration takes approximately one second.

    Now we’re ready to set up some conditional statements.  We need to first determine whether a timeout error has occurred.  If so, we should quit trying to make the connection.

    If varCounter = 30 Then

       objxmlHTTP.Abort()

       result = "Your request has timed out. Please try again later."

    We use the XMLHttpRequest object’s Abort() method to stop any transfer attempts and then set the result string variable to notify the user.

    Elseif objxmlHTTP.Status <> 200 Then

       result = objxmlHTTP.Status & ": " & objxmlHTTP.StatusText

    Now we perform a quick check to make sure that no server errors occurred.  We can do this by checking that the Status property equals 200 (or OK).

    Finally, we can process our results.

    Else

       result = objxmlHTTP.ResponseText

    End If

    Our Else statement sets our result equal to the text data returned by the XMLHttpRequest object.  We’ll finish by closing the connection (by emptying our object) and echoing the results like we did in the last example.

    Here are two example outputs after a timeout error and a server error, respectively.

    Your request has timed out.  Please try again later.

     

    404: Not Found

    I’ve included this workaround into the completely reusable function available in the source code for this article.  The code also includes an additional function for providing text-based error codes based on the ReadyState property.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · After several good discussions and a good response from the first article, I decided...
     

    WINDOWS SCRIPTING ARTICLES

    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+
    - Data Converstion and Task Addition with WPF
    - Adding Controls to an Application with WPF
    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT