Sys.Net.WebRequestExecutor executeRequest 方法

更新:2007 年 11 月

指示执行器执行 Web 请求。

MyExecutor.executeRequest();

备注

当调用此方法时,执行器打包 Web 请求实例的内容,并开始处理。

此方法可供自定义执行器使用。如果您要实现自定义执行器,应先实例化该执行器,将其分配给 Web 请求实例,然后在执行器实例上调用该方法。

在执行器生命周期的以下主要阶段需要使用 executeRequest 方法:

  • 创建和初始化执行器。

  • 调用 executeRequest,该方法执行以下操作:

    • 设置网络调用或其他处理的细节。

    • 将事件处理程序分配给请求对象。

    • 设置后台超时循环或超时观察程序。

    • 对浏览器的 XmlHttpRequest 实现进行异步调用,以将请求分派给服务器。

当请求在服务器上异步进行时,执行器实例在内存中。请求将以下列某一方法完成:

  • 执行器将其状态设置为 complete,并引发关联的 WebRequest 对象的 completed 事件。

  • 执行器将其状态设置为 timedout,并引发关联的 WebRequest 对象的 completed 事件。

  • 执行器将其状态设置为 aborted,并引发关联的 WebRequest 对象的 completed 事件。

此时将不再需要执行器,可通过调用其 dispose 方法而将其丢弃。

示例

下面的示例演示如何使用 executeRequest 方法发出 Web 请求。

// 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);
}

请参见

参考

Sys.Net.WebRequestManager 类

Sys.Net.WebRequest 类

Sys.Net.XMLHttpExecutor 类