Sys.Net.XMLHttpExecutor Class

Makes asynchronous network requests by using the browser's XMLHTTP support.

Namespace: Sys.Net

Inherits: None

var executor = new Sys.Net.XMLHttpExecutor();

Members

Name

Description

Sys.Net.XMLHttpExecutor Constructor

Initializes a new instance of the Sys.Net.XMLHttpExecutor class when implemented in a derived class.

Sys.Net.XmlHttpExecutor abort Method

Stops the pending network request issued by the executor.

Sys.Net.XmlHttpExecutor executeRequest Method

Executes a network request as specified in the associated WebRequest instance.

Sys.Net.XmlHttpExecutor.getAllResponseHeaders Method

Returns the response headers.

Sys.Net.WebRequestExecutor.getResponseHeader Method

Sys.Net.XmlHttpExecutor.getResponseHeader Method

Gets the value of a specified response header.

Sys.Net.XmlHttpExecutor aborted Property

Returns a value that indicates whether the executor was aborted.

Sys.Net.XmlHttpExecutor responseAvailable Property

Returns a value that indicates whether the network request returned without being aborted or timing out.

Sys.Net.XmlHttpExecutor responseData Property

Gets the text representation of the response body.

Sys.Net.XmlHttpExecutor started Property

Returns a value that indicates whether the executor has forwarded the request to the browser's XMLHTTP object.

Sys.Net.XmlHttpExecutor statusCode Property

Gets the status code of the browser's XMLHTTP object.

Sys.Net.XmlHttpExecutor statusText Property

Gets the status text from the browser's XMLHTTP object.

Sys.Net.XmlHttpExecutor timedOut Property

Returns a value that indicates whether the executor timed out.

Sys.Net.XmlHttpExecutor xml Property

Returns an XMLDOM object that contains the XML response from the browser's XMLHTTP object.

Remarks

The XmlHttpExecutor class acts as the default executor and is an implementation of the WebRequestExecutor abstract class.

Because the default executor is already set, you do not have to create an instance of the class and associate it with the Web request. However, if you define a custom executor, you must create an instance of the executor and set it as the default executor of the Web request.

After the network call is completed, the XmlHttpExecutor object should be used only to obtain response data, after which it should be discarded.

Note

For more information about the XMLHTTP object, see About Native XMLHTTP.

Example

The following example shows how to use the XmlHttpExecutor class methods and properties. The example shows a Web page and the client script that interacts with the XmlHttpExecutor class.

var resultElementId;

function pageLoad()
{
    resultElementId = $get("ResultId");
}

// This function aborts a Web request.
function AbortWebRequest()
{
    // Create the WebRequest object.
    wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url("getTarget.htm");

   // Clear the results area.
    resultElementId.innerHTML = "";

    // Set the Completed event handler, 
    // for processing return data
    wRequest.add_completed(OnCompleted);

    // Make the request.
    wRequest.invoke();

    // Get the current executor.
    var executor = wRequest.get_executor();


    // Abort the request.
    executor.abort();

    // Check if the executor is aborted.
    var execAborted = 
        executor.get_aborted();

    alert("Executor aborted: " + execAborted);
}

// This function executes a Web request.
function ExecuteWebRequest()
{
    // Create the WebRequest object.
    wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url("getTarget.htm");


    // Set the Completed event handler, for processing return data
    wRequest.add_completed(OnCompleted);

      // Clear the results area.
    resultElementId.innerHTML = "";

    // To use executeRequest you must instantiate the
    // executor, assign it to the Web request instance,
    // then call the executeRequest function.
    // Note: Normally to make a Web request you use
    // the invoke method of the WebRequest instance.
    var executor = new Sys.Net.XMLHttpExecutor();
    wRequest.set_executor(executor); 
    executor.executeRequest();

    var started = executor.get_started();

    alert("Executor started: " + started);
}



// This is the event handler called after 
// the Web request returns.
function OnCompleted(executor, eventArgs) 
{
    if(executor.get_responseAvailable()) 
    {

        // Get the Web request instance.
        var webReq = executor.get_webRequest();
        // Display request Url.
        alert(webReq.get_url());

       // Clear the previous results. 
       resultElementId.innerHTML = "";

       // Display the Web request status. 
       resultElementId.innerHTML +=
          "Request Status: [" + executor.get_statusCode() + " " + 
                    executor.get_statusText() + "]" + "<br/>";

        // Display the Web request headers.
        resultElementId.innerHTML += "Headers: <br/>";


        // Get all the headers.    
        resultElementId.innerHTML += 
        "All Request Headers: " +
            executor.getAllResponseHeaders() + "<br/>"; 

        // Get a specific header.
        resultElementId.innerHTML += 
        "Content-Type Header: " +
            executor.getResponseHeader("Content-Type") + 
            "<br/>";       

        // Display Web request body.
        resultElementId.innerHTML += "Body: <br/>";
        resultElementId.innerText += 
            executor.get_responseData();


    }
    else
    {
        if (executor.get_timedOut())
            alert("Timed Out");
        else
            if (executor.get_aborted())
                alert("Aborted");
    }

}

// This is the event handler called after 
// the Web request returns. It is designed
// for Web requests that return XML.
function OnSucceededXml(executor, eventArgs) 
{
    if (executor.get_responseAvailable()) 
    {
        // Display XML.
       if (document.all)
            resultElementId.innerText += executor.get_xml().xml;
        else
            // Firefox 
            resultElementId.textContent += "First node: " + 
                executor.get_xml().documentElement.nodeName;

    }
    else
    {
        if (executor.get_timedOut())
            alert("Timed Out");
        else
            if (executor.get_aborted())
                alert("Aborted");
    }
} 

// This function executes a Web request
// to get XML data.
function GetXml()
{
    // Create the WebRequest object.
    wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url("getTarget.xml");

     // Set the Completed event handler 
    // for processing return data.
    wRequest.add_completed(OnSucceededXml);

    // Clear the results area.
    resultElementId.innerText = "";

    // Invoke the Web request.
    wRequest.invoke();
}
// This function aborts a Web request.
function AbortWebRequest()
{
    // Create the WebRequest object.
    wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url("getTarget.htm");

   // Clear the results area.
    resultElementId.innerHTML = "";

    // Set the Completed event handler, 
    // for processing return data
    wRequest.add_completed(OnCompleted);

    // Make the request.
    wRequest.invoke();

    // Get the current executor.
    var executor = wRequest.get_executor();


    // Abort the request.
    executor.abort();

    // Check if the executor is aborted.
    var execAborted = 
        executor.get_aborted();

    alert("Executor aborted: " + execAborted);
}

See Also

Reference

Sys.Net.WebRequestExecutor Class

Sys.Net.WebRequestManager Class

Sys.Net.WebRequest Class