Sys.Net.WebRequestManager 类

更新:2007 年 11 月

管理由 Sys.Net.WebRequest 对象向关联的执行器对象发出的 Web 请求流。

命名空间:Sys.Net

**继承:**无

Sys.Net.WebRequestManager.memberName;

成员

名称

说明

Sys.Net.WebRequestManager 构造函数

在派生类中实现时,初始化 Sys.Net.WebRequestManager 类的新实例。

Sys.Net.WebRequestManager add_completedRequest 方法

为已完成的 WebRequestManager 请求事件注册处理程序。

Sys.Net.WebRequestManager add_invokingRequest 方法

WebRequestManager 的调用请求事件注册一个处理程序。

Sys.Net.WebRequestManager remove_completedRequest 方法

移除 add_completedRequest 方法设置的事件处理程序。

Sys.Net.WebRequestManager remove_invokingRequest 方法

移除 add_invokingRequest 方法设置的事件处理程序。

Sys.Net.WebRequestManager.executeRequest 方法

执行指定的 Web 请求。

Sys.Net.WebRequestManager defaultExecutor 属性

获取或设置 Web 请求的默认执行器。

Sys.Net.WebRequestManager defaultTimeout 属性

获取或设置 Web 请求的默认超时。

Sys.Net.WebRequestManager completedRequest 事件

在处理请求后发生。

Sys.Net.WebRequestManager invokingRequest 事件

针对请求调用处理程序函数时发生。

备注

WebRequest 对象关联的默认执行器是 XmlHttpExecutor 类的一个实例。该执行器负责发出实际网络请求。

WebRequestManager 类定义了所有 Web 请求的默认行为,因而无需为每个请求指定低级别网络配置设置。

每个页面仅包含一个 WebRequestManager 实例。但是,您可能具有 WebRequest 类的多个实例和相关的执行器。

示例

下面的示例演示如何使用 WebRequestManager 类设置通用属性以及执行 Web 请求。该示例演示了用来与 WebRequestManager 类进行交互的网页和客户端脚本。

<%@ 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();

    }

}

请参见

参考

Sys.Net.WebRequestExecutor 类

Sys.Net.XMLHttpExecutor 类

Sys.Net.WebRequest 类