Share via


Sys.Net.WebRequestManager Class

Manages the flow of the Web requests issued by the Sys.Net.WebRequest object to the associated executor object.

Namespace: Sys.Net

Inherits: None

Sys.Net.WebRequestManager.memberName;

Members

Name

Description

Sys.Net.WebRequestManager Constructor

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

Sys.Net.WebRequestManager add_completedRequest Method

Registers a handler for the completed request event of the WebRequestManager.

Sys.Net.WebRequestManager add_invokingRequest Method

Registers a handler for the invoking request event of the WebRequestManager.

Sys.Net.WebRequestManager remove_completedRequest Method

Removes the event handler set by the add_completedRequest method.

Sys.Net.WebRequestManager remove_invokingRequest Method

Removes the event handler set by the add_invokingRequest method.

Sys.Net.WebRequestManager.executeRequest Method

Executes the specified Web request.

Sys.Net.WebRequestManager defaultExecutor Property

Gets or sets the default executor for the Web request.

Sys.Net.WebRequestManager defaultTimeout Property

Gets or sets the default time-out for the Web request.

Sys.Net.WebRequestManager completedRequest Event

Occurs when a request has been handled.

Sys.Net.WebRequestManager invokingRequest Event

Occurs when a handler function is called for the request.

Remarks

The default executor associated with a WebRequest object is an instance of the XmlHttpExecutor class. The executor is responsible for making the actual network requests.

The WebRequestManager class defines the default behavior for all Web requests so that you do not have to specify low-level network configuration settings for each request.

Each page contains only one WebRequestManager instance. However, you might have several instances of the WebRequest class and related executor.

Example

The following example shows how to use the WebRequestManager class to set common properties and to execute a Web request. The example shows a Web page and the client script used to interact with the WebRequestManager class.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">

        <title> WebRequestManager  Example </title>

        <style type="text/css">
            body {  font: 11pt Trebuchet MS;
                    font-color: #000000;
                    padding-top: 72px;
                    text-align: center }

            .text { font: 8pt Trebuchet MS }
        </style>



     </head>

   <body>


    <h2>WebRequestManager Example</h2>

        <form id="form1" runat="server">
            <asp:ScriptManager runat="server" ID="scriptManagerId">
                <Scripts>
                    <asp:ScriptReference Path="WebRequestManager.js" />
                </Scripts>
            </asp:ScriptManager>
        </form>




        <table>
            <tr align="left">
                <td>Make a Web request:</td>
                <td>
                    <button id="Button1" 
                        title="adds and remove handlers, too"  
                        onclick="MakeWebRequest(); return false;">Web Request</button>
                </td>
            </tr>
            <tr align="left">
                <td>Set, get default executor:</td>
                <td>
                    <button id="Button2"  
                        onclick="DefaultExecutor(); return false;">Executor</button>
                </td>
           </tr>
           <tr align="left">
                <td>Set, get default timeout:</td>
                <td>
                    <button id="Button3" 
                        onclick="DefaultTimeout(); return false;">Timeout</button>
                </td>
           </tr>
        </table>


        <hr />

        <div id="ResultId" style="background-color:Aqua;"></div>


    </body>

</html>
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">

        <title> WebRequestManager  Example </title>

        <style type="text/css">
            body {  font: 11pt Trebuchet MS;
                    font-color: #000000;
                    padding-top: 72px;
                    text-align: center }

            .text { font: 8pt Trebuchet MS }
        </style>



     </head>

   <body>


    <h2>WebRequestManager Example</h2>

        <form id="form1" runat="server">
            <asp:ScriptManager runat="server" ID="scriptManagerId">
                <Scripts>
                    <asp:ScriptReference Path="WebRequestManager.js" />
                </Scripts>
            </asp:ScriptManager>
        </form>





        <table>
            <tr align="left">
                <td>Make a Web request:</td>
                <td>
                    <button id="Button1" 
                        title="adds and remove handlers, too"  
                        onclick="MakeWebRequest(); return false;">Web Request</button>
                </td>
            </tr>
            <tr align="left">
                <td>Set, get default executor:</td>
                <td>
                    <button id="Button2"  
                        onclick="DefaultExecutor(); return false;">Executor</button>
                </td>
           </tr>
           <tr align="left">
                <td>Set, get default timeout:</td>
                <td>
                    <button id="Button3" 
                        onclick="DefaultTimeout(); return false;">Timeout</button>
                </td>
           </tr>
        </table>


        <hr />

        <div id="ResultId" style="background-color:Aqua;"></div>


    </body>

</html>
var displayElement;

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

// Adds invokingRequest and completedRequest
// handlers, and performs a Web request. 
function MakeWebRequest() 
{
    // Clear the previous results. 
    resultElement.innerHTML = "";

    // Instantiate a Web request.
    wRequest =  new Sys.Net.WebRequest();

    // Set the handler to process the Web request.
    Sys.Net.WebRequestManager.add_completedRequest(On_WebRequestCompleted);

    alert("Added On_WebRequestCompleted handler.");

    // Set the handler to call before the Web request
    // is executed.
    Sys.Net.WebRequestManager.add_invokingRequest(On_InvokingRequest);   

    alert("Added On_InvokingRequest handler.");

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

    // Execute the request.
    // Notice that you do not use the executeRequest method of
    // the WebRequestManager which is intended for internal 
    // use only as in: Sys.Net.WebRequestManager.executeRequest(wRequest).
    // The correct way to execute a request is the following:
    // wRequest.invoke();
    Sys.Net.WebRequestManager.executeRequest(wRequest);


}

// Removes the event handlers that were previusly added. 
function RemoveDefaultHandlers() 
{
    // Clear the previous results. 
    resultElement.innerHTML = "";

    Sys.Net.WebRequestManager.remove_completedRequest(On_WebRequestCompleted);

    alert("Removed On_WebRequestCompleted handler.");

    Sys.Net.WebRequestManager.remove_invokingRequest(On_InvokingRequest); 

    alert("Removed On_InvokingRequest handler.");

}


// Gets and sets the default executor.
function DefaultExecutor()
{
    // Clear the previous results. 
    resultElement.innerHTML = "";

    // Get system default executor type.
    var sysDefaultExecutor = 
        Sys.Net.WebRequestManager.get_defaultExecutorType();
    alert("Get default executor:" + sysDefaultExecutor);


    // Modify the default executor type.
    Sys.Net.WebRequestManager.set_defaultExecutorType(
    "Sys.Net.CustomExecutor");

    var customDefaultExecutor = 
        Sys.Net.WebRequestManager.get_defaultExecutorType();

    alert("Set default executor: " + customDefaultExecutor);

    // Set the executor back to the system default. This is 
    // to allow the WebRequest script to run.
    executor = "Sys.Net.XMLHttpExecutor";
    Sys.Net.WebRequestManager.set_defaultExecutorType(
    sysDefaultExecutor);    

}


// Gets and sets the default timeout.
function DefaultTimeout()
{
    // Clear the previous results. 
    resultElement.innerHTML = "";

    // Get system default timeout.
    var sysDefaultTimeout = 
        Sys.Net.WebRequestManager.get_defaultTimeout();

    alert("Get default timeout: " + sysDefaultTimeout);


    // Set custom default timeout.
    Sys.Net.WebRequestManager.set_defaultTimeout(100);

    var customDefaultTimeout = 
        Sys.Net.WebRequestManager.get_defaultTimeout();

    alert("Set default timeout: " + customDefaultTimeout);


    // Set the timeout back to the system default. 
    Sys.Net.WebRequestManager.set_defaultTimeout(
    sysDefaultTimeout);    

}


// The On_InvokingRequest can be used to perform
// processing prior to the Web request being executed. 
function On_InvokingRequest(executor, eventArgs)
{
    alert("Executing OnInvokingRequest handler, before the Web request.");

    // Add custom code to perform processing prior
    // to the request being executed or to abort the 
    // request.
    alert("The current executor is: " + 
        executor.get_defaultExecutorType());

    // Use the eventArgs of type
    // NetworkRequestEventArgs to access the 
    // current WebRequest instance.
    var currentRequest = eventArgs.get_webRequest();
    var requestUrl = currentRequest.getResolvedUrl();           
    alert("Current request URL: " + requestUrl);
}

// The On_WebRequestComplete occurs after the
// Web request has returned, and can be used to
// get error status, process returned data, etc...
function On_WebRequestCompleted(executor, eventArgs) 
{

    if(executor.get_responseAvailable()) 
    {

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

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

        // Display Web request headers.
        resultElement.innerHTML  += 
            "Headers: ";
        resultElement.innerHTML  += 
            executor.getAllResponseHeaders() + "<br/>";

        // Display Web request body.
        resultElement.innerHTML += 
            "Body: ";

        resultElement.innerHTML  += 
           executor.get_responseData();

    }

}

See Also

Reference

Sys.Net.WebRequestExecutor Class

Sys.Net.XMLHttpExecutor Class

Sys.Net.WebRequest Class