JavaScript
  Home arrow JavaScript arrow Page 4 - Programmatic GET Requests with JavaScript:...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
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? 
JAVASCRIPT

Programmatic GET Requests with JavaScript: Simple Way to Hack Your Site
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 25
    2005-07-13

    Table of Contents:
  • Programmatic GET Requests with JavaScript: Simple Way to Hack Your Site
  • A Quick Look At The XMLHttpRequest Object
  • When High Levels of Traffic Are Dangerous
  • Automated GET requests
  • Massive HTTP requests: Using a Timer

  • 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


    Programmatic GET Requests with JavaScript: Simple Way to Hack Your Site - Automated GET requests


    (Page 4 of 5 )

    Essentially, the logic of the program is divided in two main functions. The first function that I’ll show in a moment, makes asynchronous GET requests to an specific URL, while the second display the object’s status after the request has been successfully completed. Also, a third function is used to create instances of the XMLHttpRequest object.

    Here is the first function, called “getXMLHTTPObject()”, which is defined as follows:

    // function getXMLHTTPObject
    function getXMLHTTPObject(){
        //instantiate new XMLHTTP object
        var objhttp=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
        if(!objhttp){return};
        // assign event handler
        objhttp.onreadystatechange=displayStatus;
        return objhttp;
    }

    As you can see, this function simple returns a new XMLHttpRequest object each time it’s called, and assigns to the “onreadtstatechange” event handler, the other relevant function “displayStatus()”. Notice that in case of working with “Firefox”, or “Nestcape”, the object is instantiated as a new XMLHttpRequest(), while for Internet Explorer, the object is created via an ActiveX Control.

    As I mentioned previously, here is the core function “sendRequest()”, for making requests to the server:

    function sendRequest(url,data,method,header){
        // get XMLHTTP object
        objhttp=getXMLHTTPObject();
        // set default values
        if(!url){url='default_url.htm'};
        if(!data){data='defaultdata=defaultvalue'};
        if(!method){method='get'};
        if(!header){header='Content-Type:text/html; charset=iso-8859-1'};
        // open socket connection in asynchronous mode
        objhttp.open(method,url,true);
        // send header
        objhttp.setRequestHeader(header.split(':')[0],header.split(':')[1]);
        // send data
        objhttp.send(data);
        // return xmlhttp object
        return objhttp;
    }

    In simple terms, the above function sends a GET request to the URL passed as an argument, along with the additional data that might be included into the request, as well as the http header. If no values are specified, default values are assigned for each parameter.

    Once a XMLHttpRequest object is instantiated, the function opens a connection to the given URL in asynchronous mode, which means that the script won’t wait for the server response and build the http header in the form name/value pair, to be passed to the “setRequestHeader()” method.

    Finally, the “send()” method is called, in this way completing the http request.

    Now, it’s time define the other core function “displayStatus()”, that looks like this:

    // function displayStatus
    function displayStatus(){
        if(objhttp.readyState==4){
            // create paragraph elements
            var parStat=document.createElement('p');
            var parText=document.createElement('p');
            var parResp=document.createElement('p');
            // assign ID attributes
            parStat.id='status';
            parText.id='text';
            parResp.id='response';
            // append text nodes
            parStat.appendChild(document.createTextNode('Status : '+objhttp.status));
            parText.appendChild(document.createTextNode('Status text : '+objhttp.statusText));
            parResp.appendChild(document.createTextNode('Document code : '+objhttp.responseText));
            // insert <p> elements into document tree
            document.body.appendChild(parStat);
            document.body.appendChild(parText);
            document.body.appendChild(parResp);
        }
    }

    What this functions does basically is to verify the status of the object. If the request has been successfully completed (readyState==4), then three <p> elements are dynamically created and appended to the document tree. Notice that each paragraph will display the values corresponding to each XMLHttpRequest object’s property, useful for tracking the overall request process.

    With all the three functions already defined, I can move on for writing a functional example, so keep on reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · The first part of the article is intended to show how automated JavaScript-based GET...
       · Hifirst of all thank you for your great article, it's so nice, I didn't know...
       · Thanks for the comments on the article. Pointing out to your question, the answer is...
       · Hey Alejandro, how're ya?you thanking me for the comment, come on man, thank...
       · Hello bijan,Glad to hear from you again. Despite the fact that writing a comment...
       · Hi.As usual I congratulate you for the goodarticle.I read in MDC:As a...
       · Hello Wisher,Thank you for the kind comments on my AJAX article. I’m glad you...
       · Hi.Thanks so much for the useful and readyreplay.I've just noticed the...
       · Hi Wisher,I'm glad to know my post was useful to you.Best Regards.
     

    JAVASCRIPT ARTICLES

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






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