9.3. Managing Web Requests

The WebRequest object works with a static WebRequestManager JavaScript object to coordinate the behavior of the ASP.NET AJAX networking stack. Even though you may not reference the object directly, the individual WebRequest objects that you create work with it to execute requests using the XMLHttpRequest object. In fact, the WebRequestManager does not know the specifics of the XMLHttpRequest object. Instead, it works with the WebRequestExecutor, an object that abstracts some of the underlying browser-specific details. These JavaScript objects are part of the Microsoft AJAX Library. Later in this chapter, you learn how to use a WebRequestExecutor tailored for working with XML data instead of JSON.

9.3.1. Default Timeout

Listing 9-10 (TimedSleep.aspx) modifies the Sleep.aspx page to return the amount of time that has passed during request processing instead of specific form variables. The sleep of two seconds simulates a server-side process that may require two seconds to execute.

Example 9-10. TimedSleep.aspx
<%@ Page Language="C#" %>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    DateTime start = DateTime.Now;
    System.Threading.Thread.Sleep(2000);
    DateTime finish = DateTime.Now;
    Response.Write(finish - start);
}
</script>

Listing 9-11 (CallTimedSleep.aspx) does not specify a timeout, so it gets the default value of 0, which indicates that the WebRequest should be allowed to wait indefinitely for a response from the server. This allows the two seconds of execution time to pass on the server so that the example has a reasonable value to work with instead of the near instantaneous times you would see in a development environment.

Example 9-11. CallTimedSleep.aspx
<!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>Networking</title>
<script type="text/javascript">

function pageLoad() {
    Sys.Net.WebRequestManager.add_completedRequest(completedHandler);
    var webRequest = new Sys.Net.WebRequest();
    webRequest.set_url('TimedSleep.aspx'),
    Sys.Net.WebRequestManager.executeRequest(webRequest);

}

function completedHandler(result) {
    if(result.get_responseAvailable()) {
        $get('placeholder').innerHTML += "<br />" +
            result.get_webRequest().get_url() +
            " returned " +
            result.get_responseData();
    }
}

</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="ScriptManager1">
    </asp:ScriptManager>
    <div id="placeholder">processing request</div><br />
    </form>
</body>
</html>

The result of requesting the page using Internet Explorer is shown in Figure 9-3.

Instead of explicitly setting the timeout on each WebRequest object created in the page, the default timeout for all Web requests can be set on the WebRequestManager. Listing 9-12 (SetDefaultTimeout.aspx) shows the modified pageLoad and completion handler code to demonstrate this. The default timeout is set to one second, but the page is going to sleep for two seconds, so it will always fail. In the completion event, the timeout condition is detected and a new request is created. That request will also have the one-second timeout given to the WebRequestManager.

Figure 9-3. Figure 9-3

Example 9-12. SetDefaultTimeout.aspx
<!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>Networking</title>
<script type="text/javascript">

var numberOfTries = 0;

function pageLoad() {

    Sys.Net.WebRequestManager.set_defaultTimeout(1000);
    var webRequest = new Sys.Net.WebRequest();
    webRequest.set_url('TimedSleep.aspx'),
    webRequest.add_completed(completedHandler);
    webRequest.invoke();

}

function completedHandler(result, eventArgs) {

    if(result.get_timedOut()) {
        $get('placeholder').innerText = "Timeout. Trying again after " +
numberOfTries + " times";
        numberOfTries = numberOfTries + 1;
        var anotherWebRequest = new Sys.Net.WebRequest();
        anotherWebRequest.set_url('TimedSleep.aspx'),
        anotherWebRequest.add_completed(completedHandler);

anotherWebRequest.invoke();
    }

    if(result.get_responseAvailable()) {
        $get('placeholder').innerHTML = "time taken = " +
result.get_responseData();
    }
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="ScriptManager1">
    </asp:ScriptManager>
    <div id="placeholder">processing request</div><br />
    </form>
</body>
</html>

9.3.2. Global Web Request Handling

Just as you may want to set a timeout to apply to all Web requests, you may also want to provide a single function as the completion handler for all requests. In previous examples, the add_completed method was called on the individual WebRequest, but you can see that this would become repetitious when defining a set of request objects that all use the same target.

The WebRequestManager has the add_completedRequest and remove_completedRequest methods for attaching and detaching event handlers to be used for all requests. Listing 9-13 (GlobalCompleted.aspx) is a page that calls the time.aspx and TimedSleep.aspx pages and uses a single handler for both WebRequest instances.

Example 9-13. GlobalCompleted.aspx
<!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>Networking</title>
<script type="text/javascript">


function pageLoad() {
    Sys.Net.WebRequestManager.add_completedRequest(completedHandler);

    var webRequest = new Sys.Net.WebRequest();
    webRequest.set_url('time.aspx'),

    var webRequest2 = new Sys.Net.WebRequest();

webRequest2.set_url('TimedSleep.aspx'),

    Sys.Net.WebRequestManager.executeRequest(webRequest2);
    Sys.Net.WebRequestManager.executeRequest(webRequest);

}


function completedHandler(executor) {
    if(executor.get_responseAvailable()) {
        $get('placeholder').innerHTML += "<br />" +
            executor.get_webRequest().get_url() +
            " returned " +
            executor.get_responseData ();
    }
}

</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="ScriptManager1">
    </asp:ScriptManager>
    <div id="placeholder">processing request</div><br />
    </form>
</body>
</html>

Because this code just displays the results of the call, it has a single completion handler, registered with the WebRequestManager. When you call or invoke a WebRequest, it ultimately gets processed by the WebRequestManager's executeRequest method. You can also call the executeRequest method directly, as shown in Listing 9-13.

Along with the data returned from executing the Web request, the URL of the page is also displayed. The completion handler receives the WebRequestExecutor as an argument, which has a get_webRequest method. You then have access to the original WebRequest object.

The WebRequestManager also allows you to register event handlers to be called for each request as it is about to be called:

Sys.Net.WebRequestManager.add_invokingRequest(invokingHandler);

The CancelEventArgs passed to the invokingRequest handler allows you to cancel the request before it is actually executed:

function invokingHandler(sender, eventArgs) {
    if(eventArgs.get_webRequest().get_url() === 'TimedSleep.aspx') {
        eventArgs.set_cancel(true);
    }
}

You can attach multiple event handlers for the invoking and completed events, and they will all be called. Even if one of the invoking event handlers cancels the request, the rest of the invoking handlers will still be called, so you have to be mindful of what handlers have been attached. And if you attach a completion handler directly to a WebRequest in addition to using the WebRequestManager, they will both be called.

9.3.3. The WebRequestExecutor

The WebRequestManager works with WebRequest objects to process requests using an executor object. You saw examples of using this to issue requests for .aspx pages and .asmx Web services that are only returning text. But a lot of message passing on the Web utilizes XML data for increased fidelity, flexibility, and portability. The default executor used by the WebRequestManager is XMLHttpExecutor. The WebRequestManager includes functions to get and set the default executor you want it to use:

Sys.Net.WebRequestManager.get_defaultExecutorType();
Sys.Net.WebRequestManager.set_defaultExecutorType(Sys.Net.XMLHttpExecutor);

In addition to the WebRequestExecutor, ASP.NET AJAX includes an XMLHttpExecutor that parses the XML results of a request for you. To demonstrate this process, Listing 9-14 (XMLTime.aspx) is a modification of the earlier page that returns the time from the server, but now the result is packaged as XML.

Example 9-14. XMLTime.aspx
<%@ Page Language="C#" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<?xml version="1.0" encoding="utf-8" ?>");
        Response.Write("<time><universalTime>" +
            DateTime.Now.ToUniversalTime() +
            "</universalTime></time>");
    }

</script>

When the XMLHttpExecutor is used, the results are parsed as XML and the XML is available for use in the DOM. The default executor type is the XMLHttpExecutor, but I am creating it explicitly in Listing 9-15 (XMLHttpExecutor.aspx) to show that you can call the executeRequest method on it directly. Note that in Internet Explorer, running this code will show you the XML, but in Firefox you will just see "undefined." Both browsers support the XML document returned by the get_xml() property, Firefox simply does not support the "xml" field to see the raw XML as a string.

Example 9-15. XMLHttpExecutor.aspx
<!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>Networking</title>

<script type="text/javascript">
function pageLoad() {
    Sys.Net.WebRequestManager.add_completedRequest(completedHandler);
    var webRequest = new Sys.Net.WebRequest();
    webRequest.set_url('XMLTime.aspx'),

    var executor = new Sys.Net.XMLHttpExecutor();
    webRequest.set_executor(executor);
    executor.executeRequest();
}

function completedHandler(executor) {
    if(executor.get_responseAvailable()) {

        alert(executor.get_xml().xml);
    }
}

</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="ScriptManager1">
    </asp:ScriptManager>
    </form>
</body>
</html>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset